cprover
write_goto_binary.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Write GOTO binaries
4 
5 Author: CM Wintersteiger
6 
7 \*******************************************************************/
8 
11 
12 #include "write_goto_binary.h"
13 
14 #include <fstream>
15 
16 #include <util/exception_utils.h>
17 #include <util/invariant.h>
19 #include <util/message.h>
20 #include <util/symbol_table.h>
21 
23 
26  std::ostream &out,
27  const symbol_tablet &symbol_table,
28  const goto_functionst &goto_functions,
29  irep_serializationt &irepconverter)
30 {
31  // first write symbol table
32 
33  write_gb_word(out, symbol_table.symbols.size());
34 
35  for(const auto &symbol_pair : symbol_table.symbols)
36  {
37  // Since version 2, symbols are not converted to ireps,
38  // instead they are saved in a custom binary format
39 
40  const symbolt &sym = symbol_pair.second;
41 
42  irepconverter.reference_convert(sym.type, out);
43  irepconverter.reference_convert(sym.value, out);
44  irepconverter.reference_convert(sym.location, out);
45 
46  irepconverter.write_string_ref(out, sym.name);
47  irepconverter.write_string_ref(out, sym.module);
48  irepconverter.write_string_ref(out, sym.base_name);
49  irepconverter.write_string_ref(out, sym.mode);
50  irepconverter.write_string_ref(out, sym.pretty_name);
51 
52  write_gb_word(out, 0); // old: sym.ordering
53 
54  unsigned flags=0;
55  flags = (flags << 1) | static_cast<int>(sym.is_weak);
56  flags = (flags << 1) | static_cast<int>(sym.is_type);
57  flags = (flags << 1) | static_cast<int>(sym.is_property);
58  flags = (flags << 1) | static_cast<int>(sym.is_macro);
59  flags = (flags << 1) | static_cast<int>(sym.is_exported);
60  flags = (flags << 1) | static_cast<int>(sym.is_input);
61  flags = (flags << 1) | static_cast<int>(sym.is_output);
62  flags = (flags << 1) | static_cast<int>(sym.is_state_var);
63  flags = (flags << 1) | static_cast<int>(sym.is_parameter);
64  flags = (flags << 1) | static_cast<int>(sym.is_auxiliary);
65  flags = (flags << 1) | static_cast<int>(false); // sym.binding;
66  flags = (flags << 1) | static_cast<int>(sym.is_lvalue);
67  flags = (flags << 1) | static_cast<int>(sym.is_static_lifetime);
68  flags = (flags << 1) | static_cast<int>(sym.is_thread_local);
69  flags = (flags << 1) | static_cast<int>(sym.is_file_local);
70  flags = (flags << 1) | static_cast<int>(sym.is_extern);
71  flags = (flags << 1) | static_cast<int>(sym.is_volatile);
72 
73  write_gb_word(out, flags);
74  }
75 
76  // now write functions, but only those with body
77 
78  unsigned cnt=0;
79  for(const auto &gf_entry : goto_functions.function_map)
80  {
81  if(gf_entry.second.body_available())
82  cnt++;
83  }
84 
85  write_gb_word(out, cnt);
86 
87  for(const auto &fct : goto_functions.function_map)
88  {
89  if(fct.second.body_available())
90  {
91  // Since version 2, goto functions are not converted to ireps,
92  // instead they are saved in a custom binary format
93 
94  write_gb_string(out, id2string(fct.first)); // name
95  write_gb_word(out, fct.second.body.instructions.size()); // # instructions
96 
97  for(const auto &instruction : fct.second.body.instructions)
98  {
99  irepconverter.reference_convert(instruction.code, out);
100  irepconverter.reference_convert(instruction.source_location, out);
101  write_gb_word(out, (long)instruction.type);
102  irepconverter.reference_convert(instruction.guard, out);
103  write_gb_word(out, instruction.target_number);
104 
105  write_gb_word(out, instruction.targets.size());
106 
107  for(const auto &t_it : instruction.targets)
108  write_gb_word(out, t_it->target_number);
109 
110  write_gb_word(out, instruction.labels.size());
111 
112  for(const auto &l_it : instruction.labels)
113  irepconverter.write_string_ref(out, l_it);
114  }
115  }
116  }
117 
118  // irepconverter.output_map(f);
119  // irepconverter.output_string_map(f);
120 
121  return false;
122 }
123 
126  std::ostream &out,
127  const goto_modelt &goto_model,
128  int version)
129 {
130  return write_goto_binary(
131  out,
132  goto_model.symbol_table,
133  goto_model.goto_functions,
134  version);
135 }
136 
139  std::ostream &out,
140  const symbol_tablet &symbol_table,
141  const goto_functionst &goto_functions,
142  int version)
143 {
144  // header
145  out << char(0x7f) << "GBF";
146  write_gb_word(out, version);
147 
149  irep_serializationt irepconverter(irepc);
150 
151  if(version < GOTO_BINARY_VERSION)
153  "version " + std::to_string(version) + " no longer supported",
154  "supported version = " + std::to_string(GOTO_BINARY_VERSION));
155  else if(version > GOTO_BINARY_VERSION)
157  "unknown goto binary version " + std::to_string(version),
158  "supported version = " + std::to_string(GOTO_BINARY_VERSION));
159  else
160  return write_goto_binary(out, symbol_table, goto_functions, irepconverter);
161 }
162 
165  const std::string &filename,
166  const goto_modelt &goto_model,
167  message_handlert &message_handler)
168 {
169  std::ofstream out(filename, std::ios::binary);
170 
171  if(!out)
172  {
173  messaget message(message_handler);
174  message.error() << "Failed to open '" << filename << "'";
175  return true;
176  }
177 
178  return write_goto_binary(out, goto_model);
179 }
A collection of goto functions.
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
void write_string_ref(std::ostream &, const irep_idt &)
Output a string and maintain a reference to it.
const irept & reference_convert(std::istream &)
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
const symbolst & symbols
Read-only field, used to look up symbols given their names.
The symbol table.
Definition: symbol_table.h:20
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool is_auxiliary
Definition: symbol.h:67
bool is_volatile
Definition: symbol.h:66
bool is_extern
Definition: symbol.h:66
bool is_macro
Definition: symbol.h:61
bool is_file_local
Definition: symbol.h:66
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
bool is_static_lifetime
Definition: symbol.h:65
bool is_property
Definition: symbol.h:62
bool is_parameter
Definition: symbol.h:67
bool is_thread_local
Definition: symbol.h:65
bool is_type
Definition: symbol.h:61
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
bool is_state_var
Definition: symbol.h:62
bool is_output
Definition: symbol.h:62
typet type
Type of symbol.
Definition: symbol.h:31
bool is_weak
Definition: symbol.h:67
irep_idt name
The unique identifier.
Definition: symbol.h:40
bool is_exported
Definition: symbol.h:61
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
bool is_lvalue
Definition: symbol.h:66
exprt value
Initial value of symbol.
Definition: symbol.h:34
irep_idt mode
Language mode.
Definition: symbol.h:49
bool is_input
Definition: symbol.h:62
Symbol Table + CFG.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
void write_gb_string(std::ostream &out, const std::string &s)
outputs the string and then a zero byte.
void write_gb_word(std::ostream &out, std::size_t u)
Write 7 bits of u each time, least-significant byte first, until we have zero.
binary irep conversions with hashing
static std::string binary(const constant_exprt &src)
Definition: json_expr.cpp:205
static const char * message(const statust &status)
Makes a status message string from a status.
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Author: Diffblue Ltd.
bool write_goto_binary(std::ostream &out, const symbol_tablet &symbol_table, const goto_functionst &goto_functions, irep_serializationt &irepconverter)
Writes a goto program to disc, using goto binary format.
Write GOTO binaries.
#define GOTO_BINARY_VERSION