diff --git a/jbmc/src/java_bytecode/java_bytecode_concurrency_instrumentation.cpp b/jbmc/src/java_bytecode/java_bytecode_concurrency_instrumentation.cpp index cf7e57ee3b5..2be590c8c1b 100644 --- a/jbmc/src/java_bytecode/java_bytecode_concurrency_instrumentation.cpp +++ b/jbmc/src/java_bytecode/java_bytecode_concurrency_instrumentation.cpp @@ -135,10 +135,7 @@ static void monitor_exits(codet &code, const codet &monitorexit) if(statement == ID_return) { // Replace the return with a monitor exit plus return block - code_blockt return_block; - return_block.add(monitorexit); - return_block.move_to_operands(code); - code = return_block; + code = code_blockt({monitorexit, code}); } else if( statement == ID_label || statement == ID_block || @@ -257,13 +254,7 @@ static void instrument_synchronized_code( monitor_exits(code, monitorexit); // Wrap the code into a try finally - code_blockt try_block; - try_block.move_to_operands(monitorenter); - try_block.move_to_operands(catch_push); - try_block.move_to_operands(code); - try_block.move_to_operands(catch_pop); - try_block.move_to_operands(catch_label); - code = try_block; + code = code_blockt({monitorenter, catch_push, code, catch_pop, catch_label}); } /// Transforms the codet stored in in \p f_code, which is a call to function @@ -326,17 +317,14 @@ static void instrument_start_thread( const code_assignt tmp_d(next_symbol_expr, plus); const code_assignt tmp_e(current_symbol.symbol_expr(), next_symbol_expr); - code_blockt block; - block.add(tmp_a); - block.add(tmp_b); - block.add(tmp_c); - block.add(codet(ID_atomic_begin)); - block.add(tmp_d); - block.add(tmp_e); - block.add(codet(ID_atomic_end)); - block.add_source_location() = f_code.source_location(); - - code = block; + code = code_blockt({tmp_a, + tmp_b, + tmp_c, + codet(ID_atomic_begin), + tmp_d, + tmp_e, + codet(ID_atomic_end)}); + code.add_source_location() = f_code.source_location(); } /// Transforms the codet stored in in \p f_code, which is a call to function @@ -367,12 +355,9 @@ static void instrument_end_thread( codet tmp_a(ID_end_thread); const code_labelt tmp_b(lbl2, code_skipt()); - code_blockt block; - block.add(tmp_a); - block.add(tmp_b); - block.add_source_location() = code.source_location(); - - code = block; + const auto location = code.source_location(); + code = code_blockt({tmp_a, tmp_b}); + code.add_source_location() = location; } /// Transforms the codet stored in in \p f_code, which is a call to function diff --git a/jbmc/src/java_bytecode/java_bytecode_convert_class.cpp b/jbmc/src/java_bytecode/java_bytecode_convert_class.cpp index 01a082c217e..aa14ec6553e 100644 --- a/jbmc/src/java_bytecode/java_bytecode_convert_class.cpp +++ b/jbmc/src/java_bytecode/java_bytecode_convert_class.cpp @@ -853,10 +853,7 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table) symbol_table.add(local_symbol); const auto &local_symexpr=local_symbol.symbol_expr(); - code_blockt clone_body; - code_declt declare_cloned(local_symexpr); - clone_body.move_to_operands(declare_cloned); source_locationt location; location.set_function(local_name); @@ -868,7 +865,6 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table) old_array, length_component.get_name(), length_component.type()); java_new_array.copy_to_operands(old_length); code_assignt create_blank(local_symexpr, java_new_array); - clone_body.move_to_operands(create_blank); member_exprt old_data( old_array, data_component.get_name(), data_component.type()); @@ -898,7 +894,6 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table) const auto &index_symexpr=index_symbol.symbol_expr(); code_declt declare_index(index_symexpr); - clone_body.move_to_operands(declare_index); code_fort copy_loop; @@ -920,14 +915,13 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table) code_assignt copy_cell(new_cell, old_cell); copy_loop.body()=copy_cell; - // End for-loop - clone_body.move_to_operands(copy_loop); - member_exprt new_base_class( new_array, base_class_component.get_name(), base_class_component.type()); address_of_exprt retval(new_base_class); code_returnt return_inst(retval); - clone_body.move_to_operands(return_inst); + + const code_blockt clone_body( + {declare_cloned, create_blank, declare_index, copy_loop, return_inst}); symbolt clone_symbol; clone_symbol.name=clone_name; diff --git a/jbmc/src/java_bytecode/java_bytecode_convert_method.cpp b/jbmc/src/java_bytecode/java_bytecode_convert_method.cpp index 6411c9b60ce..debdcb58250 100644 --- a/jbmc/src/java_bytecode/java_bytecode_convert_method.cpp +++ b/jbmc/src/java_bytecode/java_bytecode_convert_method.cpp @@ -873,7 +873,7 @@ code_blockt &java_bytecode_convert_methodt::get_or_create_block_for_pcrange( for(auto blockidx=child_offset, blocklim=child_offset+nblocks; blockidx!=blocklim; ++blockidx) - newblock.move_to_operands(this_block_children[blockidx]); + newblock.add(this_block_children[blockidx]); // Relabel the inner header: to_code_label(newblock.statements()[0]).set_label(new_label_irep); @@ -1727,7 +1727,7 @@ codet java_bytecode_convert_methodt::convert_instructions( { symbol_exprt lhs=tmp_variable("$stack", s_it->type()); code_assignt a(lhs, *s_it); - more_code.copy_to_operands(a); + more_code.add(a); s_it->swap(lhs); } @@ -1743,7 +1743,7 @@ codet java_bytecode_convert_methodt::convert_instructions( assert(has_prefix(os_it->get_string(ID_C_base_name), "$stack")); symbol_exprt lhs=to_symbol_expr(*os_it); code_assignt a(lhs, expr); - more_code.copy_to_operands(a); + more_code.add(a); expr.swap(lhs); ++os_it; @@ -1752,7 +1752,7 @@ codet java_bytecode_convert_methodt::convert_instructions( if(results.empty()) { - more_code.copy_to_operands(c); + more_code.add(c); c.swap(more_code); } else @@ -1834,7 +1834,7 @@ codet java_bytecode_convert_methodt::convert_instructions( if(start_new_block) { code_labelt newlabel(label(std::to_string(address)), code_blockt()); - root_block.move_to_operands(newlabel); + root_block.add(newlabel); root.branch.push_back(block_tree_nodet::get_leaf()); assert((root.branch_addresses.empty() || root.branch_addresses.back()get_statement() == ID_block) block.append(to_code_block(*expr_instrumentation)); else - block.move_to_operands(*expr_instrumentation); + block.move(*expr_instrumentation); } } @@ -350,7 +346,7 @@ void java_bytecode_instrumentt::prepend_instrumentation( if(code.get_statement()==ID_block) instrumentation.append(to_code_block(code)); else - instrumentation.copy_to_operands(code); + instrumentation.add(code); code=instrumentation; } } @@ -452,10 +448,9 @@ void java_bytecode_instrumentt::instrument_code(codet &code) // Check for a null this-argument of a virtual call: if(function_type.has_this()) { - block.copy_to_operands( - check_null_dereference( - code_function_call.arguments()[0], - code_function_call.source_location())); + block.add(check_null_dereference( + code_function_call.arguments()[0], + code_function_call.source_location())); } for(const auto &arg : code_function_call.arguments()) @@ -481,7 +476,7 @@ optionalt java_bytecode_instrumentt::instrument_expr(const exprt &expr) forall_operands(it, expr) { if(optionalt op_result = instrument_expr(*it)) - result.move_to_operands(*op_result); + result.move(*op_result); } // Add any check due at this node: @@ -503,7 +498,7 @@ optionalt java_bytecode_instrumentt::instrument_expr(const exprt &expr) dereference_expr, plus_expr.op1(), expr.source_location()); - result.move_to_operands(bounds_check); + result.move(bounds_check); } } } @@ -515,29 +510,20 @@ optionalt java_bytecode_instrumentt::instrument_expr(const exprt &expr) { // this corresponds to a throw and so we check that // we don't throw null - result.copy_to_operands( - check_null_dereference( - expr.op0(), - expr.source_location())); + result.add(check_null_dereference(expr.op0(), expr.source_location())); } else if(statement==ID_java_new_array) { // this corresponds to new array so we check that // length is >=0 - result.copy_to_operands( - check_array_length( - expr.op0(), - expr.source_location())); + result.add(check_array_length(expr.op0(), expr.source_location())); } } else if((expr.id()==ID_div || expr.id()==ID_mod) && expr.type().id()==ID_signedbv) { // check division by zero (for integer types only) - result.copy_to_operands( - check_arithmetic_exception( - expr.op1(), - expr.source_location())); + result.add(check_arithmetic_exception(expr.op1(), expr.source_location())); } else if(expr.id()==ID_dereference && expr.get_bool(ID_java_member_access)) @@ -548,7 +534,7 @@ optionalt java_bytecode_instrumentt::instrument_expr(const exprt &expr) check_null_dereference( dereference_expr.op0(), dereference_expr.source_location()); - result.move_to_operands(null_dereference_check); + result.move(null_dereference_check); } if(result==code_blockt()) @@ -599,7 +585,7 @@ void java_bytecode_instrument_symbol( /// \param exc_symbol: the top-level exception symbol /// \param source_location: the source location to attach to the assertion void java_bytecode_instrument_uncaught_exceptions( - codet &init_code, + code_blockt &init_code, const symbolt &exc_symbol, const source_locationt &source_location) { @@ -612,7 +598,7 @@ void java_bytecode_instrument_uncaught_exceptions( assert_location.set_comment("no uncaught exception"); assert_no_exception.add_source_location() = assert_location; - init_code.move_to_operands(assert_no_exception); + init_code.move(assert_no_exception); } /// Instruments all the code in the symbol_table with diff --git a/jbmc/src/java_bytecode/java_bytecode_instrument.h b/jbmc/src/java_bytecode/java_bytecode_instrument.h index 5dfd5fdd918..8000bacbae4 100644 --- a/jbmc/src/java_bytecode/java_bytecode_instrument.h +++ b/jbmc/src/java_bytecode/java_bytecode_instrument.h @@ -15,7 +15,7 @@ Date: June 2017 #include #include -class codet; +class code_blockt; void java_bytecode_instrument_symbol( symbol_table_baset &symbol_table, @@ -29,7 +29,7 @@ void java_bytecode_instrument( message_handlert &_message_handler); void java_bytecode_instrument_uncaught_exceptions( - codet &init_code, + code_blockt &init_code, const symbolt &exc_symbol, const source_locationt &source_location); diff --git a/jbmc/src/java_bytecode/java_entry_point.cpp b/jbmc/src/java_bytecode/java_entry_point.cpp index 75ceefa9c7b..91107ffa127 100644 --- a/jbmc/src/java_bytecode/java_entry_point.cpp +++ b/jbmc/src/java_bytecode/java_entry_point.cpp @@ -199,11 +199,10 @@ static void java_static_lifetime_init( set_class_identifier( to_struct_expr(zero_object), ns, to_symbol_type(sym.type)); - code_block.copy_to_operands( - code_assignt(sym.symbol_expr(), zero_object)); + code_block.add(code_assignt(sym.symbol_expr(), zero_object)); // Then call the init function: - code_block.move_to_operands(initializer_call); + code_block.move(initializer_call); } else if(sym.value.is_nil() && sym.type!=empty_typet()) { @@ -411,7 +410,7 @@ exprt::operandst java_build_arguments( input.op1()=main_arguments[param_number]; input.add_source_location()=function.location; - init_code.move_to_operands(input); + init_code.move(input); } return main_arguments; @@ -449,7 +448,7 @@ void java_record_outputs( output.op1()=return_symbol.symbol_expr(); output.add_source_location()=function.location; - init_code.move_to_operands(output); + init_code.move(output); } for(std::size_t param_number=0; @@ -472,7 +471,7 @@ void java_record_outputs( output.op1()=main_arguments[param_number]; output.add_source_location()=function.location; - init_code.move_to_operands(output); + init_code.move(output); } } @@ -490,7 +489,7 @@ void java_record_outputs( output.op1()=exc_symbol.symbol_expr(); output.add_source_location()=function.location; - init_code.move_to_operands(output); + init_code.move(output); } main_function_resultt get_main_symbol( @@ -672,7 +671,7 @@ bool generate_java_start_function( code_function_callt call_init(init_it->second.symbol_expr()); call_init.add_source_location()=symbol.location; - init_code.move_to_operands(call_init); + init_code.move(call_init); } // build call to the main method, of the form @@ -713,10 +712,9 @@ bool generate_java_start_function( symbol_table.add(exc_symbol); // Zero-initialise the top-level exception catch variable: - init_code.copy_to_operands( - code_assignt( - exc_symbol.symbol_expr(), - null_pointer_exprt(to_pointer_type(exc_symbol.type)))); + init_code.add(code_assignt( + exc_symbol.symbol_expr(), + null_pointer_exprt(to_pointer_type(exc_symbol.type)))); // create code that allocates the objects used as test arguments and // non-deterministically initializes them @@ -746,26 +744,26 @@ bool generate_java_start_function( irept catch_type_list(ID_exception_list); irept catch_target_list(ID_label); - call_block.move_to_operands(push_universal_handler); + call_block.move(push_universal_handler); // we insert the call to the method AFTER the argument initialization code - call_block.move_to_operands(call_main); + call_block.move(call_main); // Pop the handler: code_pop_catcht pop_handler; - call_block.move_to_operands(pop_handler); - init_code.move_to_operands(call_block); + call_block.move(pop_handler); + init_code.move(call_block); // Normal return: skip the exception handler: - init_code.copy_to_operands(code_gotot(after_catch.get_label())); + init_code.add(code_gotot(after_catch.get_label())); // Exceptional return: catch and assign to exc_symbol. code_landingpadt landingpad(exc_symbol.symbol_expr()); - init_code.copy_to_operands(toplevel_catch); - init_code.move_to_operands(landingpad); + init_code.add(toplevel_catch); + init_code.move(landingpad); // Converge normal and exceptional return: - init_code.move_to_operands(after_catch); + init_code.move(after_catch); // declare certain (which?) variables as test outputs java_record_outputs(symbol, main_arguments, init_code, symbol_table); diff --git a/jbmc/src/java_bytecode/java_object_factory.cpp b/jbmc/src/java_bytecode/java_object_factory.cpp index 065a08a11fc..0f98c9c2429 100644 --- a/jbmc/src/java_bytecode/java_object_factory.cpp +++ b/jbmc/src/java_bytecode/java_object_factory.cpp @@ -215,13 +215,13 @@ exprt allocate_dynamic_object( symbols_created.push_back(&malloc_sym); code_assignt assign(malloc_sym.symbol_expr(), malloc_expr); assign.add_source_location()=loc; - output_code.copy_to_operands(assign); + output_code.add(assign); exprt malloc_symbol_expr=malloc_sym.symbol_expr(); if(cast_needed) malloc_symbol_expr=typecast_exprt(malloc_symbol_expr, target_expr.type()); code_assignt code(target_expr, malloc_symbol_expr); code.add_source_location()=loc; - output_code.copy_to_operands(code); + output_code.add(code); return malloc_sym.symbol_expr(); } else @@ -230,7 +230,7 @@ exprt allocate_dynamic_object( null_pointer_exprt null_pointer_expr(to_pointer_type(target_expr.type())); code_assignt code(target_expr, null_pointer_expr); code.add_source_location()=loc; - output_code.copy_to_operands(code); + output_code.add(code); return exprt(); } } @@ -321,7 +321,7 @@ exprt java_object_factoryt::allocate_object( aoe=typecast_exprt(aoe, target_expr.type()); code_assignt code(target_expr, aoe); code.add_source_location()=loc; - assignments.copy_to_operands(code); + assignments.add(code); return aoe; } case allocation_typet::DYNAMIC: @@ -775,8 +775,7 @@ void java_object_factoryt::gen_nondet_pointer_init( { if(update_in_place==update_in_placet::NO_UPDATE_IN_PLACE) { - assignments.copy_to_operands( - get_null_assignment(expr, pointer_type)); + assignments.add(get_null_assignment(expr, pointer_type)); } // Otherwise leave it as it is. return; @@ -1056,8 +1055,7 @@ void java_object_factoryt::gen_nondet_struct_init( symbol_table, object_factory_parameters.string_printable); - assignments.copy_to_operands( - code_assignt(expr, initial_object)); + assignments.add(code_assignt(expr, initial_object)); } for(const auto &component : components) @@ -1081,7 +1079,7 @@ void java_object_factoryt::gen_nondet_struct_init( continue; code_assignt code(me, from_integer(0, me.type())); code.add_source_location() = loc; - assignments.copy_to_operands(code); + assignments.add(code); } else if(skip_special_string_fields && (name == "length" || name == "data")) { @@ -1239,7 +1237,7 @@ void java_object_factoryt::gen_nondet_init( code_assignt assign(expr, rhs); assign.add_source_location()=loc; - assignments.copy_to_operands(assign); + assignments.add(assign); } } @@ -1330,7 +1328,7 @@ void java_object_factoryt::allocate_nondet_length_array( java_new_array.type().subtype().set(ID_element_type, element_type); code_assignt assign(lhs, java_new_array); assign.add_source_location() = loc; - assignments.copy_to_operands(assign); + assignments.add(assign); } /// Create code to initialize a Java array whose size will be at most @@ -1395,7 +1393,7 @@ void java_object_factoryt::gen_nondet_array_init( const auto &array_init_symexpr=array_init_symbol.symbol_expr(); code_assignt data_assign(array_init_symexpr, init_array_expr); data_assign.add_source_location()=loc; - assignments.copy_to_operands(data_assign); + assignments.add(data_assign); // Emit init loop for(array_init_iter=0; array_init_iter!=array.length; // ++array_init_iter) init(array[array_init_iter]); @@ -1410,13 +1408,13 @@ void java_object_factoryt::gen_nondet_array_init( exprt counter_expr=counter.symbol_expr(); exprt java_zero=from_integer(0, java_int_type()); - assignments.copy_to_operands(code_assignt(counter_expr, java_zero)); + assignments.add(code_assignt(counter_expr, java_zero)); std::string head_name = id2string(counter.base_name) + "_header"; code_labelt init_head_label(head_name, code_skipt()); code_gotot goto_head(head_name); - assignments.move_to_operands(init_head_label); + assignments.move(init_head_label); std::string done_name = id2string(counter.base_name) + "_done"; code_labelt init_done_label(done_name, code_skipt()); @@ -1426,7 +1424,7 @@ void java_object_factoryt::gen_nondet_array_init( done_test.cond()=equal_exprt(counter_expr, length_expr); done_test.then_case()=goto_done; - assignments.move_to_operands(done_test); + assignments.move(done_test); if(update_in_place!=update_in_placet::MUST_UPDATE_IN_PLACE) { @@ -1436,7 +1434,7 @@ void java_object_factoryt::gen_nondet_array_init( max_test.cond()=equal_exprt(counter_expr, max_length_expr); max_test.then_case()=goto_done; - assignments.move_to_operands(max_test); + assignments.move(max_test); } const dereference_exprt arraycellref( @@ -1467,9 +1465,9 @@ void java_object_factoryt::gen_nondet_array_init( exprt java_one=from_integer(1, java_int_type()); code_assignt incr(counter_expr, plus_exprt(counter_expr, java_one)); - assignments.move_to_operands(incr); - assignments.move_to_operands(goto_head); - assignments.move_to_operands(init_done_label); + assignments.move(incr); + assignments.move(goto_head); + assignments.move(init_done_label); } /// We nondet-initialize enums to be equal to one of the constants defined diff --git a/jbmc/src/java_bytecode/java_static_initializers.cpp b/jbmc/src/java_bytecode/java_static_initializers.cpp index 0f1d69bd8dd..80c5a62be60 100644 --- a/jbmc/src/java_bytecode/java_static_initializers.cpp +++ b/jbmc/src/java_bytecode/java_static_initializers.cpp @@ -208,6 +208,7 @@ static void clinit_wrapper_do_recursive_calls( auto findit = symbol_table.symbols.find(base_init_routine); if(findit == symbol_table.symbols.end()) continue; + const code_function_callt call_base(findit->second.symbol_expr()); init_body.add(call_base); } @@ -689,7 +690,7 @@ codet get_clinit_wrapper_body( // add the "already-run = false" statement code_blockt init_body; code_assignt set_already_run(already_run_symbol.symbol_expr(), true_exprt()); - init_body.move_to_operands(set_already_run); + init_body.move(set_already_run); clinit_wrapper_do_recursive_calls( symbol_table, diff --git a/jbmc/src/java_bytecode/remove_java_new.cpp b/jbmc/src/java_bytecode/remove_java_new.cpp index 26b48b637d9..35ba1e8996b 100644 --- a/jbmc/src/java_bytecode/remove_java_new.cpp +++ b/jbmc/src/java_bytecode/remove_java_new.cpp @@ -295,13 +295,13 @@ goto_programt::targett remove_java_newt::lower_java_new_array( .symbol_expr(); code_declt init_decl(init_sym); init_decl.add_source_location() = location; - for_body.move_to_operands(init_decl); + for_body.move(init_decl); code_assignt init_subarray(init_sym, sub_java_new); code_assignt assign_subarray( deref_expr, typecast_exprt(init_sym, deref_expr.type())); - for_body.move_to_operands(init_subarray); - for_body.move_to_operands(assign_subarray); + for_body.move(init_subarray); + for_body.move(assign_subarray); for_loop.init() = code_assignt(tmp_i, from_integer(0, tmp_i.type())); for_loop.cond() = binary_relation_exprt(tmp_i, ID_lt, rhs.op0()); diff --git a/jbmc/src/java_bytecode/simple_method_stubbing.cpp b/jbmc/src/java_bytecode/simple_method_stubbing.cpp index c1fabbc2c6c..ad891d1f663 100644 --- a/jbmc/src/java_bytecode/simple_method_stubbing.cpp +++ b/jbmc/src/java_bytecode/simple_method_stubbing.cpp @@ -170,7 +170,7 @@ void java_simple_method_stubst::create_method_stub(symbolt &symbol) code_assignt get_argument( init_symbol_expression, symbol_exprt(this_argument.get_identifier())); get_argument.add_source_location() = synthesized_source_location; - new_instructions.copy_to_operands(get_argument); + new_instructions.add(get_argument); create_method_stub_at( this_type, init_symbol_expression, @@ -220,7 +220,7 @@ void java_simple_method_stubst::create_method_stub(symbolt &symbol) 0, false, false); - new_instructions.copy_to_operands(code_returnt(to_return)); + new_instructions.add(code_returnt(to_return)); } } diff --git a/src/ansi-c/ansi_c_entry_point.cpp b/src/ansi-c/ansi_c_entry_point.cpp index db03b94672f..27c8e3b2d8d 100644 --- a/src/ansi-c/ansi_c_entry_point.cpp +++ b/src/ansi-c/ansi_c_entry_point.cpp @@ -73,7 +73,7 @@ void record_function_outputs( output.op1()=return_symbol.symbol_expr(); output.add_source_location()=function.location; - init_code.move_to_operands(output); + init_code.move(output); } #if 0 @@ -215,7 +215,7 @@ bool generate_ansi_c_start_function( code_function_callt call_init(init_it->second.symbol_expr()); call_init.add_source_location()=symbol.location; - init_code.move_to_operands(call_init); + init_code.move(call_init); } // build call to main function @@ -260,7 +260,7 @@ bool generate_ansi_c_start_function( const binary_relation_exprt ge(argc_symbol.symbol_expr(), ID_ge, one); code_assumet assumption(ge); - init_code.move_to_operands(assumption); + init_code.move(assumption); } { @@ -274,7 +274,7 @@ bool generate_ansi_c_start_function( argc_symbol.symbol_expr(), ID_le, bound_expr); code_assumet assumption(le); - init_code.move_to_operands(assumption); + init_code.move(assumption); } { @@ -284,7 +284,7 @@ bool generate_ansi_c_start_function( input.op0()=address_of_exprt( index_exprt(string_constantt("argc"), from_integer(0, index_type()))); input.op1()=argc_symbol.symbol_expr(); - init_code.move_to_operands(input); + init_code.move(input); } if(parameters.size()==3) @@ -311,7 +311,7 @@ bool generate_ansi_c_start_function( envp_size_symbol.symbol_expr(), ID_le, max_minus_one); code_assumet assumption(le); - init_code.move_to_operands(assumption); + init_code.move(assumption); } { @@ -346,7 +346,7 @@ bool generate_ansi_c_start_function( // disable bounds check on that one index_expr.set("bounds_check", false); - init_code.copy_to_operands(code_assignt(index_expr, null)); + init_code.add(code_assignt(index_expr, null)); } if(parameters.size()==3) @@ -366,7 +366,7 @@ bool generate_ansi_c_start_function( const equal_exprt is_null(index_expr, null); code_assumet assumption2(is_null); - init_code.move_to_operands(assumption2); + init_code.move(assumption2); } { @@ -430,7 +430,7 @@ bool generate_ansi_c_start_function( symbol_table); } - init_code.move_to_operands(call_main); + init_code.move(call_main); // TODO: add read/modified (recursively in call graph) globals as INPUT/OUTPUT diff --git a/src/ansi-c/c_typecheck_code.cpp b/src/ansi-c/c_typecheck_code.cpp index bbdc4326384..a77b23bc9e3 100644 --- a/src/ansi-c/c_typecheck_code.cpp +++ b/src/ansi-c/c_typecheck_code.cpp @@ -188,7 +188,7 @@ void c_typecheck_baset::typecheck_block(codet &code) // do decl-blocks - exprt new_ops; + code_blockt new_ops; new_ops.operands().reserve(code.operands().size()); Forall_operands(it1, code) @@ -211,10 +211,10 @@ void c_typecheck_baset::typecheck_block(codet &code) // codet &label_op=*code_ptr; - new_ops.move_to_operands(code_op); + new_ops.move(code_op); } else - new_ops.move_to_operands(code_op); + new_ops.move(code_op); } code.operands().swap(new_ops.operands()); @@ -353,7 +353,7 @@ void c_typecheck_baset::typecheck_decl(codet &code) else { // build a decl-block - code_blockt code_block(new_code); + auto code_block=code_blockt::from_list(new_code); code_block.set_statement(ID_decl_block); code.swap(code_block); } @@ -453,7 +453,7 @@ void c_typecheck_baset::typecheck_for(codet &code) code_blockt code_block; code_block.add_source_location()=code.op3().source_location(); - code_block.move_to_operands(code.op3()); + code_block.move(to_code(code.op3())); code.op3().swap(code_block); } typecheck_code(to_code(code.op3())); @@ -481,9 +481,9 @@ void c_typecheck_baset::typecheck_for(codet &code) } code_block.reserve_operands(2); - code_block.move_to_operands(code.op0()); + code_block.move(to_code(code.op0())); code.op0().make_nil(); - code_block.move_to_operands(code); + code_block.move(code); code.swap(code_block); typecheck_code(code); // recursive call } @@ -624,11 +624,9 @@ void c_typecheck_baset::typecheck_ifthenelse(code_ifthenelset &code) if(code.then_case().get_statement() == ID_decl_block) { - code_blockt code_block; + code_blockt code_block({code.then_case()}); code_block.add_source_location()=code.then_case().source_location(); - - code_block.move_to_operands(code.then_case()); - code.then_case().swap(code_block); + code.then_case() = code_block; } typecheck_code(code.then_case()); @@ -637,11 +635,9 @@ void c_typecheck_baset::typecheck_ifthenelse(code_ifthenelset &code) { if(code.else_case().get_statement() == ID_decl_block) { - code_blockt code_block; + code_blockt code_block({code.else_case()}); code_block.add_source_location()=code.else_case().source_location(); - - code_block.move_to_operands(code.else_case()); - code.else_case().swap(code_block); + code.else_case() = code_block; } typecheck_code(code.else_case()); @@ -757,11 +753,9 @@ void c_typecheck_baset::typecheck_while(code_whilet &code) if(code.body().get_statement()==ID_decl_block) { - code_blockt code_block; + code_blockt code_block({code.body()}); code_block.add_source_location()=code.body().source_location(); - - code_block.move_to_operands(code.body()); - code.body().swap(code_block); + code.body() = code_block; } typecheck_code(code.body()); @@ -792,11 +786,9 @@ void c_typecheck_baset::typecheck_dowhile(code_dowhilet &code) if(code.body().get_statement()==ID_decl_block) { - code_blockt code_block; + code_blockt code_block({code.body()}); code_block.add_source_location()=code.body().source_location(); - - code_block.move_to_operands(code.body()); - code.body().swap(code_block); + code.body() = code_block; } typecheck_code(code.body()); diff --git a/src/ansi-c/c_typecheck_expr.cpp b/src/ansi-c/c_typecheck_expr.cpp index cabe02402a9..d3eb466ddc4 100644 --- a/src/ansi-c/c_typecheck_expr.cpp +++ b/src/ansi-c/c_typecheck_expr.cpp @@ -1010,7 +1010,7 @@ void c_typecheck_baset::typecheck_expr_sizeof(exprt &expr) { side_effect_exprt side_effect_expr( ID_statement_expression, void_type(), expr.source_location()); - code_blockt decl_block(clean_code); + auto decl_block=code_blockt::from_list(clean_code); decl_block.set_statement(ID_decl_block); side_effect_expr.copy_to_operands(decl_block); clean_code.clear(); @@ -1067,7 +1067,7 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr) { side_effect_exprt side_effect_expr( ID_statement_expression, void_type(), expr.source_location()); - code_blockt decl_block(clean_code); + auto decl_block=code_blockt::from_list(clean_code); decl_block.set_statement(ID_decl_block); side_effect_expr.copy_to_operands(decl_block); clean_code.clear(); diff --git a/src/cpp/cpp_constructor.cpp b/src/cpp/cpp_constructor.cpp index f99db77e03e..fc90ae02074 100644 --- a/src/cpp/cpp_constructor.cpp +++ b/src/cpp/cpp_constructor.cpp @@ -193,7 +193,7 @@ optionalt cpp_typecheckt::cpp_constructor( assign.move_to_operands(member, val); typecheck_side_effect_assignment(assign); code_expressiont code_exp(assign); - block.move_to_operands(code_exp); + block.move(code_exp); } // enter struct scope diff --git a/src/cpp/cpp_typecheck.cpp b/src/cpp/cpp_typecheck.cpp index dbf17736b6e..7fa663ea3a7 100644 --- a/src/cpp/cpp_typecheck.cpp +++ b/src/cpp/cpp_typecheck.cpp @@ -176,8 +176,7 @@ void cpp_typecheckt::static_and_dynamic_initialization() { // This will be a constructor call, // which we execute. - assert(symbol.value.id()==ID_code); - init_block.copy_to_operands(symbol.value); + init_block.add(to_code(symbol.value)); // Make it nil to get zero initialization by // __CPROVER_initialize @@ -191,7 +190,7 @@ void cpp_typecheckt::static_and_dynamic_initialization() auto call = cpp_constructor(symbol.location, symbol_expr, ops); if(call.has_value()) - init_block.move(call.value()); + init_block.add(call.value()); } } diff --git a/src/cpp/cpp_typecheck_code.cpp b/src/cpp/cpp_typecheck_code.cpp index 2176710d089..26f411c6adb 100644 --- a/src/cpp/cpp_typecheck_code.cpp +++ b/src/cpp/cpp_typecheck_code.cpp @@ -160,8 +160,7 @@ void cpp_typecheckt::typecheck_switch(code_switcht &code) c_typecheck_baset::typecheck_switch(code); - code_blockt code_block; - code_block.move_to_operands(decl.op0(), code); + code_blockt code_block({to_code(decl.op0()), code}); code.swap(code_block); } else diff --git a/src/cpp/cpp_typecheck_destructor.cpp b/src/cpp/cpp_typecheck_destructor.cpp index e0327c212ce..274bb1e8f25 100644 --- a/src/cpp/cpp_typecheck_destructor.cpp +++ b/src/cpp/cpp_typecheck_destructor.cpp @@ -127,7 +127,7 @@ codet cpp_typecheckt::dtor(const symbolt &symbol) disable_access_control = disabled_access_control; if(dtor_code.has_value()) - block.move_to_operands(dtor_code.value()); + block.add(dtor_code.value()); } if(symbol.type.id() == ID_union) @@ -153,7 +153,7 @@ codet cpp_typecheckt::dtor(const symbolt &symbol) disable_access_control = disabled_access_control; if(dtor_code.has_value()) - block.move_to_operands(dtor_code.value()); + block.add(dtor_code.value()); } return block; diff --git a/src/goto-instrument/goto_program2code.cpp b/src/goto-instrument/goto_program2code.cpp index 5247706b54d..579dd5fa1e4 100644 --- a/src/goto-instrument/goto_program2code.cpp +++ b/src/goto-instrument/goto_program2code.cpp @@ -506,7 +506,7 @@ goto_programt::const_targett goto_program2codet::convert_decl( if(move_to_dest) dest.move_to_operands(d); else - toplevel_block.move_to_operands(d); + toplevel_block.add(d); return target; } @@ -1748,9 +1748,7 @@ void goto_program2codet::cleanup_code_ifthenelse( { // we re-introduce 1-code blocks with if-then-else to avoid dangling-else // ambiguity - code_blockt b; - b.move_to_operands(i_t_e.then_case()); - i_t_e.then_case().swap(b); + i_t_e.then_case() = code_blockt({i_t_e.then_case()}); } if( @@ -1760,9 +1758,7 @@ void goto_program2codet::cleanup_code_ifthenelse( { // we re-introduce 1-code blocks with if-then-else to avoid dangling-else // ambiguity - code_blockt b; - b.move_to_operands(i_t_e.else_case()); - i_t_e.else_case().swap(b); + i_t_e.else_case() = code_blockt({i_t_e.else_case()}); } } @@ -1779,11 +1775,7 @@ void goto_program2codet::cleanup_code_ifthenelse( if(moved) { - code_blockt b; - b.move_to_operands(i_t_e); - b.move_to_operands(then_label); - b.move_to_operands(else_label); - code.swap(b); + code = code_blockt({i_t_e, then_label, else_label}); cleanup_code(code, parent_stmt); } } diff --git a/src/jsil/jsil_entry_point.cpp b/src/jsil/jsil_entry_point.cpp index 4b8aa6c0197..8ac7034bc7f 100644 --- a/src/jsil/jsil_entry_point.cpp +++ b/src/jsil/jsil_entry_point.cpp @@ -132,7 +132,7 @@ bool jsil_entry_point( code_function_callt call_init(init_it->second.symbol_expr()); call_init.add_source_location()=symbol.location; - init_code.move_to_operands(call_init); + init_code.add(call_init); } // build call to main function @@ -141,7 +141,7 @@ bool jsil_entry_point( call_main.add_source_location()=symbol.location; call_main.function().add_source_location()=symbol.location; - init_code.move_to_operands(call_main); + init_code.add(call_main); // add "main" symbolt new_symbol; diff --git a/src/linking/static_lifetime_init.cpp b/src/linking/static_lifetime_init.cpp index 0bd3254fbae..ab9102b37f2 100644 --- a/src/linking/static_lifetime_init.cpp +++ b/src/linking/static_lifetime_init.cpp @@ -127,7 +127,7 @@ void static_lifetime_init( code_assignt code(symbol.symbol_expr(), rhs); code.add_source_location()=symbol.location; - dest.move_to_operands(code); + dest.add(code); } // call designated "initialization" functions @@ -146,7 +146,7 @@ void static_lifetime_init( { code_function_callt function_call(symbol.symbol_expr()); function_call.add_source_location()=source_location; - dest.move_to_operands(function_call); + dest.add(function_call); } } } diff --git a/src/util/expr.h b/src/util/expr.h index a0e24c948e9..a89cc19a630 100644 --- a/src/util/expr.h +++ b/src/util/expr.h @@ -121,6 +121,13 @@ class exprt:public irept operands().push_back(expr); } + /// Copy the given argument to the end of `exprt`'s operands. + /// \param expr: `exprt` to append to the operands + void copy_to_operands(exprt &&expr) + { + operands().push_back(std::move(expr)); + } + /// Copy the given arguments to the end of `exprt`'s operands. /// \param e1: first `exprt` to append to the operands /// \param e2: second `exprt` to append to the operands @@ -134,6 +141,19 @@ class exprt:public irept op.push_back(e2); } + /// Copy the given arguments to the end of `exprt`'s operands. + /// \param e1: first `exprt` to append to the operands + /// \param e2: second `exprt` to append to the operands + void copy_to_operands(exprt &&e1, exprt &&e2) + { + operandst &op = operands(); + #ifndef USE_LIST + op.reserve(op.size() + 2); + #endif + op.push_back(std::move(e1)); + op.push_back(std::move(e2)); + } + /// Copy the given arguments to the end of `exprt`'s operands. /// \param e1: first `exprt` to append to the operands /// \param e2: second `exprt` to append to the operands @@ -149,6 +169,21 @@ class exprt:public irept op.push_back(e3); } + /// Copy the given arguments to the end of `exprt`'s operands. + /// \param e1: first `exprt` to append to the operands + /// \param e2: second `exprt` to append to the operands + /// \param e3: third `exprt` to append to the operands + void copy_to_operands(exprt &&e1, exprt &&e2, exprt &&e3) + { + operandst &op = operands(); + #ifndef USE_LIST + op.reserve(op.size() + 3); + #endif + op.push_back(std::move(e1)); + op.push_back(std::move(e2)); + op.push_back(std::move(e3)); + } + void make_typecast(const typet &_type); void make_not(); diff --git a/src/util/std_code.cpp b/src/util/std_code.cpp index 018701d99e2..8ff7cdbdae3 100644 --- a/src/util/std_code.cpp +++ b/src/util/std_code.cpp @@ -119,15 +119,42 @@ void code_blockt::append(const code_blockt &extra_block) } } +codet &code_blockt::find_last_statement() +{ + codet *last=this; + + while(true) + { + const irep_idt &statement=last->get_statement(); + + if(statement==ID_block && + !to_code_block(*last).statements().empty()) + { + last=&to_code_block(*last).statements().back(); + } + else if(statement==ID_label) + { + DATA_INVARIANT( + last->operands().size() == 1, "label must have one operand"); + last=&(to_code(last->op0())); + } + else + break; + } + + return *last; +} + code_blockt create_fatal_assertion( const exprt &condition, const source_locationt &loc) { - code_blockt result; - result.copy_to_operands(code_assertt(condition)); - result.copy_to_operands(code_assumet(condition)); - for(auto &statement : result.statements()) - statement.add_source_location() = loc; + code_blockt result({code_assertt(condition), code_assumet(condition)}); + + for(auto &op : result.statements()) + op.add_source_location() = loc; + result.add_source_location() = loc; + return result; } diff --git a/src/util/std_code.h b/src/util/std_code.h index 2290f0d18d7..be2255a7cd6 100644 --- a/src/util/std_code.h +++ b/src/util/std_code.h @@ -117,15 +117,24 @@ class code_blockt:public codet return (const code_operandst &)get_sub(); } - explicit code_blockt(const std::list &_list):codet(ID_block) + static code_blockt from_list(const std::list &_list) { - auto &o = statements(); - reserve_operands(_list.size()); - for(std::list::const_iterator - it=_list.begin(); - it!=_list.end(); - it++) - o.push_back(*it); + code_blockt result; + auto &s=result.statements(); + s.reserve(_list.size()); + for(const auto &c : _list) + s.push_back(c); + return result; + } + + explicit code_blockt(const std::vector &_statements):codet(ID_block) + { + operands()=(const std::vector &)_statements; + } + + explicit code_blockt(std::vector &&_statements):codet(ID_block) + { + operands()=std::move((std::vector &&)_statements); } void move(codet &code) @@ -138,6 +147,11 @@ class code_blockt:public codet copy_to_operands(code); } + void add(codet &&code) + { + copy_to_operands(std::move(code)); + } + void add(codet code, const source_locationt &loc) { code.add_source_location() = loc; @@ -152,31 +166,7 @@ class code_blockt:public codet return static_cast(find(ID_C_end_location)); } - codet &find_last_statement() - { - codet *last=this; - - while(true) - { - const irep_idt &statement=last->get_statement(); - - if(statement==ID_block && - !last->operands().empty()) - { - last=&to_code(last->operands().back()); - } - else if(statement==ID_label) - { - DATA_INVARIANT( - last->operands().size() == 1, "label must have one operand"); - last=&(to_code(last->op0())); - } - else - break; - } - - return *last; - } + codet &find_last_statement(); }; template<> inline bool can_cast_expr(const exprt &base)