CBMC
goto_program2code.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Dump Goto-Program as C/C++ Source
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_GOTO_INSTRUMENT_GOTO_PROGRAM2CODE_H
13 #define CPROVER_GOTO_INSTRUMENT_GOTO_PROGRAM2CODE_H
14 
15 #include <list>
16 #include <unordered_set>
17 
18 #include <analyses/natural_loops.h>
19 
20 #include <util/std_code.h>
21 
23 {
24  typedef std::list<irep_idt> id_listt;
25  typedef std::map<goto_programt::const_targett, goto_programt::const_targett>
27  typedef std::unordered_map<irep_idt, unsigned> dead_mapt;
28  typedef std::list<std::pair<goto_programt::const_targett, bool> >
30 
31  struct caset
32  {
33  const exprt value; // condition upon which this case is taken
36  goto_programt::const_targett case_last; // last instruction of case
37 
39  const exprt &v,
42  value(v), case_selector(sel), case_start(st),
43  case_last(goto_program.instructions.end())
44  {
45  }
46  };
47  typedef std::list<caset> cases_listt;
48 
49 public:
51  const irep_idt &identifier,
52  const goto_programt &_goto_program,
53  symbol_tablet &_symbol_table,
54  code_blockt &_dest,
55  id_listt &_local_static,
56  id_listt &_type_names,
57  const std::unordered_set<irep_idt> &_typedef_names,
58  std::set<std::string> &_system_headers)
59  : func_name(identifier),
60  goto_program(_goto_program),
61  symbol_table(_symbol_table),
62  ns(_symbol_table),
63  toplevel_block(_dest),
64  local_static(_local_static),
65  type_names(_type_names),
66  typedef_names(_typedef_names),
67  system_headers(_system_headers)
68  {
69  assert(local_static.empty());
70 
71  for(id_listt::const_iterator
72  it=type_names.begin();
73  it!=type_names.end();
74  ++it)
75  type_names_set.insert(*it);
76  }
77 
78  void operator()();
79 
80 protected:
84  const namespacet ns;
88  const std::unordered_set<irep_idt> &typedef_names;
89  std::set<std::string> &system_headers;
90  std::unordered_set<exprt, irep_hash> va_list_expr;
91 
94  std::unordered_set<irep_idt> labels_in_use;
97  std::unordered_set<irep_idt> local_static_set;
98  std::unordered_set<irep_idt> type_names_set;
99  std::unordered_set<irep_idt> const_removed;
100 
102 
103  void build_loop_map();
104  void build_dead_map();
105  void scan_for_varargs();
106 
107  void cleanup_code(codet &code, const irep_idt parent_stmt);
108 
110  const exprt &function,
112 
113  void cleanup_code_block(
114  codet &code,
115  const irep_idt parent_stmt);
116 
118  codet &code,
119  const irep_idt parent_stmt);
120 
121  void cleanup_expr(exprt &expr, bool no_typecast);
122 
123  void add_local_types(const typet &type);
124 
125  void remove_const(typet &type);
126 
129  goto_programt::const_targett upper_bound,
130  code_blockt &dest);
131 
133 
136  goto_programt::const_targett upper_bound,
137  code_blockt &dest);
138 
141  goto_programt::const_targett upper_bound,
142  code_blockt &dest);
143 
144  void convert_assign_rec(const code_assignt &assign, code_blockt &dest);
145 
148  goto_programt::const_targett upper_bound,
149  code_blockt &dest);
150 
153  goto_programt::const_targett upper_bound,
154  code_blockt &dest);
155 
159  code_blockt &dest);
160 
163  goto_programt::const_targett upper_bound,
164  code_blockt &dest);
165 
169  code_blockt &dest);
170 
173  goto_programt::const_targett upper_bound,
174  code_blockt &dest);
175 
178  goto_programt::const_targett upper_bound,
179  const exprt &switch_var,
180  cases_listt &cases,
181  goto_programt::const_targett &first_target,
182  goto_programt::const_targett &default_target);
183 
185  goto_programt::const_targett upper_bound,
186  const cfg_dominatorst &dominators,
187  cases_listt &cases,
188  std::set<unsigned> &processed_locations);
189 
190  bool remove_default(
191  const cfg_dominatorst &dominators,
192  const cases_listt &cases,
193  goto_programt::const_targett default_target);
194 
197  goto_programt::const_targett upper_bound,
198  code_blockt &dest);
199 
202  goto_programt::const_targett upper_bound,
203  code_blockt &dest);
204 
207 
210  goto_programt::const_targett upper_bound,
211  code_blockt &dest);
212 
215 
218  goto_programt::const_targett upper_bound,
219  code_blockt &dest);
220 };
221 
222 #endif // CPROVER_GOTO_INSTRUMENT_GOTO_PROGRAM2CODE_H
goto_program2codet::caset::caset
caset(const goto_programt &goto_program, const exprt &v, goto_programt::const_targett sel, goto_programt::const_targett st)
Definition: goto_program2code.h:38
goto_program2codet::caset::value
const exprt value
Definition: goto_program2code.h:33
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:129
goto_program2codet::va_list_expr
std::unordered_set< exprt, irep_hash > va_list_expr
Definition: goto_program2code.h:90
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
goto_program2codet::cleanup_code_ifthenelse
void cleanup_code_ifthenelse(codet &code, const irep_idt parent_stmt)
Definition: goto_program2code.cpp:1654
goto_program2codet::remove_default
bool remove_default(const cfg_dominatorst &dominators, const cases_listt &cases, goto_programt::const_targett default_target)
Definition: goto_program2code.cpp:800
goto_program2codet::convert_goto_break_continue
goto_programt::const_targett convert_goto_break_continue(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:1104
goto_program2codet::loopt
std::map< goto_programt::const_targett, goto_programt::const_targett > loopt
Definition: goto_program2code.h:26
goto_program2codet::cleanup_function_call
void cleanup_function_call(const exprt &function, code_function_callt::argumentst &arguments)
Definition: goto_program2code.cpp:1490
goto_program2codet::convert_goto
goto_programt::const_targett convert_goto(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:535
typet
The type of an expression, extends irept.
Definition: type.h:28
goto_program2codet::convert_assign_rec
void convert_assign_rec(const code_assignt &assign, code_blockt &dest)
Definition: goto_program2code.cpp:395
goto_program2codet::const_removed
std::unordered_set< irep_idt > const_removed
Definition: goto_program2code.h:99
goto_program2codet::caset
Definition: goto_program2code.h:31
goto_program2codet::copy_source_location
void copy_source_location(goto_programt::const_targett, codet &dst)
Definition: goto_program2code.cpp:1951
goto_program2codet::loops
natural_loopst loops
Definition: goto_program2code.h:92
goto_program2codet::cleanup_expr
void cleanup_expr(exprt &expr, bool no_typecast)
Definition: goto_program2code.cpp:1744
goto_program2codet::labels_in_use
std::unordered_set< irep_idt > labels_in_use
Definition: goto_program2code.h:94
goto_program2codet::convert_instruction
goto_programt::const_targett convert_instruction(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:134
exprt
Base class for all expressions.
Definition: expr.h:55
goto_program2codet::local_static_set
std::unordered_set< irep_idt > local_static_set
Definition: goto_program2code.h:97
goto_program2codet::type_names_set
std::unordered_set< irep_idt > type_names_set
Definition: goto_program2code.h:98
goto_program2codet::goto_program2codet
goto_program2codet(const irep_idt &identifier, const goto_programt &_goto_program, symbol_tablet &_symbol_table, code_blockt &_dest, id_listt &_local_static, id_listt &_type_names, const std::unordered_set< irep_idt > &_typedef_names, std::set< std::string > &_system_headers)
Definition: goto_program2code.h:50
goto_program2codet::convert_catch
goto_programt::const_targett convert_catch(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:1355
goto_program2codet::add_local_types
void add_local_types(const typet &type)
Definition: goto_program2code.cpp:1365
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
goto_program2codet::toplevel_block
code_blockt & toplevel_block
Definition: goto_program2code.h:85
goto_program2codet::convert_start_thread
goto_programt::const_targett convert_start_thread(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:1230
goto_program2codet::build_dead_map
void build_dead_map()
Definition: goto_program2code.cpp:87
goto_program2codet::convert_goto_if
goto_programt::const_targett convert_goto_if(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:1027
goto_program2codet::func_name
const irep_idt & func_name
Definition: goto_program2code.h:81
goto_program2codet::convert_goto_goto
goto_programt::const_targett convert_goto_goto(goto_programt::const_targett target, code_blockt &dest)
Definition: goto_program2code.cpp:1175
goto_program2codet::ns
const namespacet ns
Definition: goto_program2code.h:84
goto_program2codet::type_names
id_listt & type_names
Definition: goto_program2code.h:87
goto_program2codet::cleanup_code
void cleanup_code(codet &code, const irep_idt parent_stmt)
Definition: goto_program2code.cpp:1403
goto_program2codet::loop_last_stack
loop_last_stackt loop_last_stack
Definition: goto_program2code.h:96
natural_loopst
A concretized version of natural_loops_templatet<const goto_programt, goto_programt::const_targett>
Definition: natural_loops.h:83
goto_program2codet::convert_decl
goto_programt::const_targett convert_decl(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:447
code_function_callt::argumentst
exprt::operandst argumentst
Definition: goto_instruction_code.h:281
goto_program2codet::scan_for_varargs
void scan_for_varargs()
Definition: goto_program2code.cpp:102
goto_program2codet::dead_mapt
std::unordered_map< irep_idt, unsigned > dead_mapt
Definition: goto_program2code.h:27
goto_program2codet::get_cases
goto_programt::const_targett get_cases(goto_programt::const_targett target, goto_programt::const_targett upper_bound, const exprt &switch_var, cases_listt &cases, goto_programt::const_targett &first_target, goto_programt::const_targett &default_target)
Definition: goto_program2code.cpp:648
std_code.h
goto_program2codet::dead_map
dead_mapt dead_map
Definition: goto_program2code.h:95
goto_program2codet::convert_set_return_value
goto_programt::const_targett convert_set_return_value(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:417
goto_program2codet::loop_last_stackt
std::list< std::pair< goto_programt::const_targett, bool > > loop_last_stackt
Definition: goto_program2code.h:29
goto_program2codet::goto_program
const goto_programt & goto_program
Definition: goto_program2code.h:82
goto_program2codet::caset::case_start
goto_programt::const_targett case_start
Definition: goto_program2code.h:35
goto_program2codet::convert_labels
void convert_labels(goto_programt::const_targett target, code_blockt &dest)
Definition: goto_program2code.cpp:244
goto_program2codet::convert_assign_varargs
goto_programt::const_targett convert_assign_varargs(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:304
goto_program2codet::symbol_table
symbol_tablet & symbol_table
Definition: goto_program2code.h:83
goto_program2codet::convert_throw
goto_programt::const_targett convert_throw(goto_programt::const_targett target, code_blockt &dest)
Definition: goto_program2code.cpp:1346
natural_loops.h
goto_program2codet::convert_goto_switch
goto_programt::const_targett convert_goto_switch(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:857
goto_program2codet
Definition: goto_program2code.h:22
goto_program2codet::id_listt
std::list< irep_idt > id_listt
Definition: goto_program2code.h:24
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
goto_program2codet::operator()
void operator()()
Definition: goto_program2code.cpp:24
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:587
goto_program2codet::caset::case_last
goto_programt::const_targett case_last
Definition: goto_program2code.h:36
goto_program2codet::local_static
id_listt & local_static
Definition: goto_program2code.h:86
goto_program2codet::set_block_end_points
bool set_block_end_points(goto_programt::const_targett upper_bound, const cfg_dominatorst &dominators, cases_listt &cases, std::set< unsigned > &processed_locations)
Definition: goto_program2code.cpp:750
goto_program2codet::system_headers
std::set< std::string > & system_headers
Definition: goto_program2code.h:89
goto_program2codet::loop_map
loopt loop_map
Definition: goto_program2code.h:93
goto_program2codet::remove_const
void remove_const(typet &type)
Definition: goto_program2code.cpp:1576
goto_program2codet::convert_goto_while
goto_programt::const_targett convert_goto_while(goto_programt::const_targett target, goto_programt::const_targett loop_end, code_blockt &dest)
Definition: goto_program2code.cpp:558
code_assignt
A goto_instruction_codet representing an assignment in the program.
Definition: goto_instruction_code.h:22
goto_program2codet::caset::case_selector
goto_programt::const_targett case_selector
Definition: goto_program2code.h:34
goto_program2codet::convert_do_while
goto_programt::const_targett convert_do_while(goto_programt::const_targett target, goto_programt::const_targett loop_end, code_blockt &dest)
Definition: goto_program2code.cpp:510
goto_program2codet::cleanup_code_block
void cleanup_code_block(codet &code, const irep_idt parent_stmt)
Definition: goto_program2code.cpp:1524
goto_program2codet::cases_listt
std::list< caset > cases_listt
Definition: goto_program2code.h:47
goto_program2codet::typedef_names
const std::unordered_set< irep_idt > & typedef_names
Definition: goto_program2code.h:88
goto_program2codet::convert_assign
goto_programt::const_targett convert_assign(goto_programt::const_targett target, goto_programt::const_targett upper_bound, code_blockt &dest)
Definition: goto_program2code.cpp:289
goto_program2codet::build_loop_map
void build_loop_map()
Definition: goto_program2code.cpp:51
cfg_dominators_templatet< const goto_programt, goto_programt::const_targett, false >
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:28