CBMC
skip_loops.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Skip over selected loops by adding gotos
4 
5 Author: Michael Tautschnig
6 
7 Date: January 2016
8 
9 \*******************************************************************/
10 
13 
14 #include "skip_loops.h"
15 
16 #include <util/message.h>
17 #include <util/string2int.h>
18 
20 
21 typedef std::set<unsigned> loop_idst;
22 typedef std::map<irep_idt, loop_idst> loop_mapt;
23 
24 static bool skip_loops(
25  goto_programt &goto_program,
26  const loop_idst &loop_ids,
27  messaget &message)
28 {
29  loop_idst::const_iterator l_it=loop_ids.begin();
30  Forall_goto_program_instructions(it, goto_program)
31  {
32  if(l_it==loop_ids.end())
33  break;
34  if(!it->is_backwards_goto())
35  continue;
36 
37  const unsigned loop_id=it->loop_number;
38  if(*l_it<loop_id)
39  break; // error handled below
40  if(*l_it>loop_id)
41  continue;
42 
43  goto_programt::targett loop_head=it->get_target();
44  goto_programt::targett next=it;
45  ++next;
46  assert(next!=goto_program.instructions.end());
47 
48  goto_program.insert_before(
49  loop_head,
51  next, true_exprt(), loop_head->source_location()));
52 
53  ++l_it;
54  }
55  if(l_it!=loop_ids.end())
56  {
57  message.error() << "Loop " << *l_it << " not found"
58  << messaget::eom;
59  return true;
60  }
61 
62  return false;
63 }
64 
65 static bool parse_loop_ids(
66  const std::string &loop_ids,
67  loop_mapt &loop_map)
68 {
69  std::string::size_type length=loop_ids.length();
70 
71  for(std::string::size_type idx=0; idx<length; idx++)
72  {
73  std::string::size_type next=loop_ids.find(",", idx);
74  std::string val=loop_ids.substr(idx, next-idx);
75  std::string::size_type delim=val.rfind(".");
76 
77  if(delim==std::string::npos)
78  return true;
79 
80  std::string fn=val.substr(0, delim);
81  unsigned nr=safe_string2unsigned(val.substr(delim+1));
82 
83  loop_map[fn].insert(nr);
84 
85  if(next==std::string::npos)
86  break;
87  idx=next;
88  }
89 
90  return false;
91 }
92 
94  goto_modelt &goto_model,
95  const std::string &loop_ids,
96  message_handlert &message_handler)
97 {
98  messaget message(message_handler);
99 
100  loop_mapt loop_map;
101  if(parse_loop_ids(loop_ids, loop_map))
102  {
103  message.error() << "Failed to parse loop ids" << messaget::eom;
104  return true;
105  }
106 
107  loop_mapt::const_iterator it=loop_map.begin();
108  for(auto &gf_entry : goto_model.goto_functions.function_map)
109  {
110  if(it == loop_map.end() || it->first < gf_entry.first)
111  break; // possible error handled below
112  else if(it->first == gf_entry.first)
113  {
114  if(skip_loops(gf_entry.second.body, it->second, message))
115  return true;
116  ++it;
117  }
118  }
119  if(it!=loop_map.end())
120  {
121  message.error() << "No function " << it->first << " in goto program"
122  << messaget::eom;
123  return true;
124  }
125 
126  // update counters etc.
127  goto_model.goto_functions.update();
128 
129  return false;
130 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
Forall_goto_program_instructions
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:1234
parse_loop_ids
static bool parse_loop_ids(const std::string &loop_ids, loop_mapt &loop_map)
Definition: skip_loops.cpp:65
loop_mapt
std::map< irep_idt, loop_idst > loop_mapt
Definition: skip_loops.cpp:22
skip_loops.h
irept::find
const irept & find(const irep_idt &name) const
Definition: irep.cpp:106
skip_loops
static bool skip_loops(goto_programt &goto_program, const loop_idst &loop_ids, messaget &message)
Definition: skip_loops.cpp:24
goto_model.h
loop_idst
std::set< unsigned > loop_idst
Definition: skip_loops.cpp:21
goto_modelt
Definition: goto_model.h:25
messaget::eom
static eomt eom
Definition: message.h:297
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
goto_programt::make_goto
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:1030
string2int.h
goto_programt::insert_before
targett insert_before(const_targett target)
Insertion before the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:662
messaget::error
mstreamt & error() const
Definition: message.h:399
message_handlert
Definition: message.h:27
safe_string2unsigned
unsigned safe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:16
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:592
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
goto_functionst::update
void update()
Definition: goto_functions.h:83
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
size_type
unsignedbv_typet size_type()
Definition: c_types.cpp:68
message.h
true_exprt
The Boolean constant true.
Definition: std_expr.h:3007
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:211
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586