Skip to content

Commit 464d8eb

Browse files
authored
Merge pull request #8175 from tautschnig/cleanup/mm_io-sequence
MMIO instrumentation cleanup
2 parents 5d85d38 + 7ee921b commit 464d8eb

File tree

7 files changed

+77
-38
lines changed

7 files changed

+77
-38
lines changed

Diff for: doc/architectural/goto-program-transformations.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ See the device behaviour section of `modeling-mmio.md` for details of
6262
modeling memory-mapped I/O regions of device interfaces. This pass is always
6363
carried out but will only make changes if one of the modelling functions exist.
6464

65-
The implementation of this pass is called via the \ref mm_io(goto_modelt &)
65+
The implementation of this pass is called via the
66+
\ref mm_io(goto_modelt &, message_handlert &)
6667
"mm_io" function. Further documentation of this pass can be found in \ref
6768
mm_io.h
6869

Diff for: jbmc/src/jdiff/jdiff_parse_options.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ bool jdiff_parse_optionst::process_goto_program(
190190
class_hierarchyt class_hierarchy(goto_model.symbol_table);
191191
remove_instanceof(goto_model, class_hierarchy, ui_message_handler);
192192

193-
mm_io(goto_model);
193+
mm_io(goto_model, ui_message_handler);
194194

195195
// instrument library preconditions
196196
instrument_preconditions(goto_model);

Diff for: regression/cbmc/mm_io1/test.desc

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CORE
22
main.c
33
--no-standard-checks
4+
^Replaced MMIO operations: 3 read\(s\), 1 write\(s\)
45
^EXIT=0$
56
^SIGNAL=0$
67
^VERIFICATION SUCCESSFUL$

Diff for: src/goto-instrument/goto_instrument_parse_options.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Author: Daniel Kroening, [email protected]
2929
#include <goto-programs/label_function_pointer_call_sites.h>
3030
#include <goto-programs/link_to_library.h>
3131
#include <goto-programs/loop_ids.h>
32+
#include <goto-programs/mm_io.h>
3233
#include <goto-programs/parameter_assignments.h>
3334
#include <goto-programs/read_goto_binary.h>
3435
#include <goto-programs/remove_calls_no_body.h>
@@ -899,6 +900,7 @@ int goto_instrument_parse_optionst::doit()
899900
if(cmdline.isset("drop-unused-functions"))
900901
{
901902
do_indirect_call_and_rtti_removal();
903+
mm_io(goto_model, ui_message_handler);
902904

903905
log.status() << "Removing unused functions" << messaget::eom;
904906
remove_unused_functions(goto_model.goto_functions, ui_message_handler);

Diff for: src/goto-programs/mm_io.cpp

+67-33
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Date: April 2017
1414
#include "mm_io.h"
1515

1616
#include <util/fresh_symbol.h>
17+
#include <util/message.h>
1718
#include <util/pointer_expr.h>
1819
#include <util/pointer_offset_size.h>
1920
#include <util/pointer_predicates.h>
@@ -23,6 +24,51 @@ Date: April 2017
2324

2425
#include <set>
2526

27+
class mm_iot
28+
{
29+
public:
30+
explicit mm_iot(symbol_table_baset &symbol_table);
31+
32+
void mm_io(goto_functionst::goto_functiont &goto_function);
33+
34+
std::size_t reads_replaced = 0;
35+
std::size_t writes_replaced = 0;
36+
37+
protected:
38+
const irep_idt id_r = CPROVER_PREFIX "mm_io_r";
39+
const irep_idt id_w = CPROVER_PREFIX "mm_io_w";
40+
41+
const namespacet ns;
42+
exprt mm_io_r;
43+
exprt mm_io_r_value;
44+
exprt mm_io_w;
45+
};
46+
47+
mm_iot::mm_iot(symbol_table_baset &symbol_table)
48+
: ns(symbol_table),
49+
mm_io_r(nil_exprt{}),
50+
mm_io_r_value(nil_exprt{}),
51+
mm_io_w(nil_exprt{})
52+
{
53+
if(const auto mm_io_r_symbol = symbol_table.lookup(id_r))
54+
{
55+
mm_io_r = mm_io_r_symbol->symbol_expr();
56+
57+
const auto &value_symbol = get_fresh_aux_symbol(
58+
to_code_type(mm_io_r.type()).return_type(),
59+
id2string(id_r) + "$value",
60+
id2string(id_r) + "$value",
61+
mm_io_r_symbol->location,
62+
mm_io_r_symbol->mode,
63+
symbol_table);
64+
65+
mm_io_r_value = value_symbol.symbol_expr();
66+
}
67+
68+
if(const auto mm_io_w_symbol = symbol_table.lookup(id_w))
69+
mm_io_w = mm_io_w_symbol->symbol_expr();
70+
}
71+
2672
static std::set<dereference_exprt> collect_deref_expr(const exprt &src)
2773
{
2874
std::set<dereference_exprt> collected;
@@ -33,13 +79,12 @@ static std::set<dereference_exprt> collect_deref_expr(const exprt &src)
3379
return collected;
3480
}
3581

36-
void mm_io(
37-
const exprt &mm_io_r,
38-
const exprt &mm_io_r_value,
39-
const exprt &mm_io_w,
40-
goto_functionst::goto_functiont &goto_function,
41-
const namespacet &ns)
82+
void mm_iot::mm_io(goto_functionst::goto_functiont &goto_function)
4283
{
84+
// return early when there is nothing to be done
85+
if(mm_io_r.is_nil() && mm_io_w.is_nil())
86+
return;
87+
4388
for(auto it = goto_function.body.instructions.begin();
4489
it != goto_function.body.instructions.end();
4590
it++)
@@ -76,6 +121,7 @@ void mm_io(
76121
typecast_exprt(size_opt.value(), st)},
77122
source_location);
78123
goto_function.body.insert_before_swap(it, call);
124+
++reads_replaced;
79125
it++;
80126
}
81127
}
@@ -99,45 +145,33 @@ void mm_io(
99145
typecast_exprt(a_rhs, vt)});
100146
goto_function.body.insert_before_swap(it);
101147
*it = goto_programt::make_function_call(fc, source_location);
148+
++writes_replaced;
102149
it++;
103150
}
104151
}
105152
}
106153
}
107154

108-
void mm_io(symbol_tablet &symbol_table, goto_functionst &goto_functions)
155+
void mm_io(
156+
symbol_tablet &symbol_table,
157+
goto_functionst &goto_functions,
158+
message_handlert &message_handler)
109159
{
110-
const namespacet ns(symbol_table);
111-
exprt mm_io_r = nil_exprt();
112-
exprt mm_io_r_value = nil_exprt();
113-
exprt mm_io_w = nil_exprt();
160+
mm_iot rewrite{symbol_table};
114161

115-
const irep_idt id_r = CPROVER_PREFIX "mm_io_r";
116-
const irep_idt id_w = CPROVER_PREFIX "mm_io_w";
162+
for(auto &f : goto_functions.function_map)
163+
rewrite.mm_io(f.second);
117164

118-
if(const auto mm_io_r_symbol = symbol_table.lookup(id_r))
165+
if(rewrite.reads_replaced || rewrite.writes_replaced)
119166
{
120-
mm_io_r = mm_io_r_symbol->symbol_expr();
121-
122-
const auto &value_symbol = get_fresh_aux_symbol(
123-
to_code_type(mm_io_r.type()).return_type(),
124-
id2string(id_r) + "$value",
125-
id2string(id_r) + "$value",
126-
mm_io_r_symbol->location,
127-
mm_io_r_symbol->mode,
128-
symbol_table);
129-
130-
mm_io_r_value = value_symbol.symbol_expr();
167+
messaget log{message_handler};
168+
log.status() << "Replaced MMIO operations: " << rewrite.reads_replaced
169+
<< " read(s), " << rewrite.writes_replaced << " write(s)"
170+
<< messaget::eom;
131171
}
132-
133-
if(const auto mm_io_w_symbol = symbol_table.lookup(id_w))
134-
mm_io_w = mm_io_w_symbol->symbol_expr();
135-
136-
for(auto & f : goto_functions.function_map)
137-
mm_io(mm_io_r, mm_io_r_value, mm_io_w, f.second, ns);
138172
}
139173

140-
void mm_io(goto_modelt &model)
174+
void mm_io(goto_modelt &model, message_handlert &message_handler)
141175
{
142-
mm_io(model.symbol_table, model.goto_functions);
176+
mm_io(model.symbol_table, model.goto_functions, message_handler);
143177
}

Diff for: src/goto-programs/mm_io.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ Date: April 2017
3131

3232
class goto_functionst;
3333
class goto_modelt;
34+
class message_handlert;
3435
class symbol_tablet;
3536

36-
void mm_io(const symbol_tablet &, goto_functionst &);
37-
void mm_io(goto_modelt &);
37+
void mm_io(const symbol_tablet &, goto_functionst &, message_handlert &);
38+
void mm_io(goto_modelt &, message_handlert &);
3839

3940
#endif // CPROVER_GOTO_PROGRAMS_MM_IO_H

Diff for: src/goto-programs/process_goto_program.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool process_goto_program(
4545
<< messaget::eom;
4646
remove_function_pointers(log.get_message_handler(), goto_model, false);
4747

48-
mm_io(goto_model);
48+
mm_io(goto_model, log.get_message_handler());
4949

5050
// instrument library preconditions
5151
instrument_preconditions(goto_model);

0 commit comments

Comments
 (0)