@@ -19,15 +19,12 @@ Author: CM Wintersteiger
1919
2020#include < goto-programs/goto_model.h>
2121
22- // / Writes a goto program to disc, using goto binary format
23- bool write_goto_binary (
22+ // / Writes the symbol table to file.
23+ static void write_symbol_table_binary (
2424 std::ostream &out,
2525 const symbol_table_baset &symbol_table,
26- const goto_functionst &goto_functions,
2726 irep_serializationt &irepconverter)
2827{
29- // first write symbol table
30-
3128 write_gb_word (out, symbol_table.symbols .size ());
3229
3330 for (const auto &symbol_pair : symbol_table.symbols )
@@ -70,9 +67,45 @@ bool write_goto_binary(
7067
7168 write_gb_word (out, flags);
7269 }
70+ }
71+
72+ static void write_instructions_binary (
73+ std::ostream &out,
74+ irep_serializationt &irepconverter,
75+ const std::pair<const irep_idt, goto_functiont> &fct)
76+ {
77+ write_gb_word (out, fct.second .body .instructions .size ());
78+
79+ for (const auto &instruction : fct.second .body .instructions )
80+ {
81+ irepconverter.reference_convert (instruction.code (), out);
82+ irepconverter.reference_convert (instruction.source_location (), out);
83+ write_gb_word (out, (long )instruction.type ());
84+
85+ const auto condition =
86+ instruction.has_condition () ? instruction.condition () : true_exprt ();
87+ irepconverter.reference_convert (condition, out);
7388
74- // now write functions, but only those with body
89+ write_gb_word (out, instruction. target_number );
7590
91+ write_gb_word (out, instruction.targets .size ());
92+
93+ for (const auto &t_it : instruction.targets )
94+ write_gb_word (out, t_it->target_number );
95+
96+ write_gb_word (out, instruction.labels .size ());
97+
98+ for (const auto &l_it : instruction.labels )
99+ irepconverter.write_string_ref (out, l_it);
100+ }
101+ }
102+
103+ // / Writes the functions to file, but only those with non-empty body.
104+ static void write_goto_functions_binary (
105+ std::ostream &out,
106+ const goto_functionst &goto_functions,
107+ irep_serializationt &irepconverter)
108+ {
76109 unsigned cnt=0 ;
77110 for (const auto &gf_entry : goto_functions.function_map )
78111 {
@@ -84,43 +117,27 @@ bool write_goto_binary(
84117
85118 for (const auto &fct : goto_functions.function_map )
86119 {
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
120+ if (!fct.second .body_available ())
121+ continue ;
91122
92- write_gb_string (out, id2string (fct.first )); // name
93- write_gb_word (out, fct.second .body .instructions .size ()); // # instructions
94-
95- for (const auto &instruction : fct.second .body .instructions )
96- {
97- irepconverter.reference_convert (instruction.code (), out);
98- irepconverter.reference_convert (instruction.source_location (), out);
99- write_gb_word (out, (long )instruction.type ());
100-
101- const auto condition =
102- instruction.has_condition () ? instruction.condition () : true_exprt ();
103- irepconverter.reference_convert (condition, out);
104-
105- write_gb_word (out, instruction.target_number );
106-
107- write_gb_word (out, instruction.targets .size ());
108-
109- for (const auto &t_it : instruction.targets )
110- write_gb_word (out, t_it->target_number );
111-
112- write_gb_word (out, instruction.labels .size ());
123+ // Since version 2, goto functions are not converted to ireps,
124+ // instead they are saved in a custom binary format
113125
114- for (const auto &l_it : instruction.labels )
115- irepconverter.write_string_ref (out, l_it);
116- }
117- }
126+ const auto function_name = id2string (fct.first );
127+ write_gb_string (out, function_name);
128+ write_instructions_binary (out, irepconverter, fct);
118129 }
130+ }
119131
120- // irepconverter.output_map(f);
121- // irepconverter.output_string_map(f);
122-
123- return false ;
132+ // / Writes a goto program to disc, using goto binary format
133+ static void write_goto_binary (
134+ std::ostream &out,
135+ const symbol_table_baset &symbol_table,
136+ const goto_functionst &goto_functions,
137+ irep_serializationt &irepconverter)
138+ {
139+ write_symbol_table_binary (out, symbol_table, irepconverter);
140+ write_goto_functions_binary (out, goto_functions, irepconverter);
124141}
125142
126143// / Writes a goto program to disc
@@ -151,15 +168,19 @@ bool write_goto_binary(
151168 irep_serializationt irepconverter (irepc);
152169
153170 if (version < GOTO_BINARY_VERSION)
171+ {
154172 throw invalid_command_line_argument_exceptiont (
155173 " version " + std::to_string (version) + " no longer supported" ,
156174 " supported version = " + std::to_string (GOTO_BINARY_VERSION));
157- else if (version > GOTO_BINARY_VERSION)
175+ }
176+ if (version > GOTO_BINARY_VERSION)
177+ {
158178 throw invalid_command_line_argument_exceptiont (
159179 " unknown goto binary version " + std::to_string (version),
160180 " supported version = " + std::to_string (GOTO_BINARY_VERSION));
161- else
162- return write_goto_binary (out, symbol_table, goto_functions, irepconverter);
181+ }
182+ write_goto_binary (out, symbol_table, goto_functions, irepconverter);
183+ return false ;
163184}
164185
165186// / Writes a goto program to disc
0 commit comments