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  forall_goto_functions(it, goto_functions)
80  if(it->second.body_available())
81  cnt++;
82 
83  write_gb_word(out, cnt);
84 
85  for(const auto &fct : goto_functions.function_map)
86  {
87  if(fct.second.body_available())
88  {
89  // Since version 2, goto functions are not converted to ireps,
90  // instead they are saved in a custom binary format
91 
92  write_gb_string(out, id2string(fct.first)); // name
93  write_gb_word(out, fct.second.body.instructions.size()); // # instructions
94 
95  forall_goto_program_instructions(i_it, fct.second.body)
96  {
97  const goto_programt::instructiont &instruction = *i_it;
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 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
exception_utils.h
GOTO_BINARY_VERSION
#define GOTO_BINARY_VERSION
Definition: write_goto_binary.h:15
symbolt::is_state_var
bool is_state_var
Definition: symbol.h:62
irep_serialization.h
binary irep conversions with hashing
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
goto_programt::instructiont::source_location
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:269
symbolt::is_macro
bool is_macro
Definition: symbol.h:61
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
write_goto_binary
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.
Definition: write_goto_binary.cpp:25
goto_programt::instructiont::type
goto_program_instruction_typet type
What kind of instruction?
Definition: goto_program.h:272
symbolt::is_input
bool is_input
Definition: symbol.h:62
invariant.h
irep_serializationt::reference_convert
const irept & reference_convert(std::istream &)
Definition: irep_serialization.cpp:44
goto_model.h
Symbol Table + CFG.
goto_modelt
Definition: goto_model.h:26
irep_serializationt::ireps_containert
Definition: irep_serialization.h:32
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:55
goto_programt::instructiont::targets
targetst targets
The list of successor instructions.
Definition: goto_program.h:306
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
write_goto_binary.h
Write GOTO binaries.
symbolt::pretty_name
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
message
static const char * message(const static_verifier_resultt::statust &status)
Makes a status message string from a status.
Definition: static_verifier.cpp:74
symbolt::is_thread_local
bool is_thread_local
Definition: symbol.h:65
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
goto_programt::instructiont::code
codet code
Do not read or modify directly – use get_X() instead.
Definition: goto_program.h:182
write_gb_string
void write_gb_string(std::ostream &out, const std::string &s)
outputs the string and then a zero byte.
Definition: irep_serialization.cpp:183
goto_programt::instructiont::labels
labelst labels
Definition: goto_program.h:331
write_gb_word
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.
Definition: irep_serialization.cpp:135
symbolt::is_exported
bool is_exported
Definition: symbol.h:61
symbolt::is_parameter
bool is_parameter
Definition: symbol.h:67
message_handlert
Definition: message.h:28
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
goto_programt::instructiont::target_number
unsigned target_number
A number to identify branch targets.
Definition: goto_program.h:534
symbolt::is_output
bool is_output
Definition: symbol.h:62
symbolt::is_extern
bool is_extern
Definition: symbol.h:66
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
symbolt::is_volatile
bool is_volatile
Definition: symbol.h:66
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:28
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
symbolt::is_type
bool is_type
Definition: symbol.h:61
goto_programt::instructiont::guard
exprt guard
Guard for gotos, assume, assert Use get_condition() to read, and set_condition(c) to write.
Definition: goto_program.h:276
symbolt::is_auxiliary
bool is_auxiliary
Definition: symbol.h:67
binary
static std::string binary(const constant_exprt &src)
Definition: json_expr.cpp:204
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
forall_goto_functions
#define forall_goto_functions(it, functions)
Definition: goto_functions.h:122
symbolt::is_file_local
bool is_file_local
Definition: symbol.h:66
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
symbol_table.h
Author: Diffblue Ltd.
message.h
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
invalid_command_line_argument_exceptiont
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Definition: exception_utils.h:38
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:179
irep_serializationt
Definition: irep_serialization.h:29
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
symbolt::is_weak
bool is_weak
Definition: symbol.h:67
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
symbolt::is_property
bool is_property
Definition: symbol.h:62
forall_goto_program_instructions
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:1196
irep_serializationt::write_string_ref
void write_string_ref(std::ostream &, const irep_idt &)
Output a string and maintain a reference to it.
Definition: irep_serialization.cpp:224