|
15 | 15 | #include <util/arith_tools.h>
|
16 | 16 | #include <util/ieee_float.h>
|
17 | 17 |
|
| 18 | +/// String to indicate that there is no value. |
18 | 19 | #define NO_VALUE "(none)"
|
19 | 20 |
|
20 | 21 | /// Prints a constant to the given output stream.
|
21 |
| -/// \param [out] out: Stream that should receive the result. |
| 22 | +/// \param [out] os: Stream that should receive the result. |
22 | 23 | /// \param constant: Constant that shall be printed.
|
23 |
| -static void output_constant(std::ostream &out, const constant_exprt &constant) |
| 24 | +static void output_constant(std::ostream &os, const constant_exprt &constant) |
24 | 25 | {
|
25 | 26 | mp_integer ivalue;
|
26 | 27 | if(!to_integer(constant, ivalue))
|
27 |
| - out << ivalue; |
| 28 | + os << ivalue; |
28 | 29 | else if(can_cast_type<floatbv_typet>(constant.type()))
|
29 | 30 | {
|
30 | 31 | ieee_floatt real{get_real_type()};
|
31 | 32 | real.from_expr(constant);
|
32 |
| - out << real.to_float(); |
| 33 | + os << real.to_float(); |
33 | 34 | }
|
34 | 35 | else
|
35 |
| - out << constant.get_value(); |
| 36 | + os << constant.get_value(); |
| 37 | +} |
| 38 | + |
| 39 | +/// Prints the assignment of a module parameter to the given output stream. |
| 40 | +/// \param [out] os: Stream that should receive the result. |
| 41 | +/// \param assignment: Assignment that shall be printed. |
| 42 | +static void |
| 43 | +output_parameter_assignment(std::ostream &os, const equal_exprt &assignment) |
| 44 | +{ |
| 45 | + os << assignment.lhs().get(ID_identifier) << " := "; |
| 46 | + const constant_exprt *const constant = |
| 47 | + expr_try_dynamic_cast<constant_exprt>(assignment.rhs()); |
| 48 | + if(constant) |
| 49 | + output_constant(os, *constant); |
| 50 | + else |
| 51 | + os << assignment.rhs().get(ID_identifier); |
36 | 52 | }
|
37 | 53 |
|
38 | 54 | void output_parse_tree(
|
@@ -65,176 +81,178 @@ void output_parse_tree(
|
65 | 81 | }
|
66 | 82 |
|
67 | 83 | void output_function_block(
|
68 |
| - std::ostream &out, |
| 84 | + std::ostream &os, |
69 | 85 | const statement_list_parse_treet::function_blockt &function_block)
|
70 | 86 | {
|
71 |
| - output_tia_module_properties(function_block, out); |
72 |
| - output_common_var_declarations(out, function_block); |
73 |
| - output_static_var_declarations(out, function_block); |
74 |
| - output_network_list(out, function_block.networks); |
| 87 | + output_tia_module_properties(function_block, os); |
| 88 | + output_common_var_declarations(os, function_block); |
| 89 | + output_static_var_declarations(os, function_block); |
| 90 | + output_network_list(os, function_block.networks); |
75 | 91 | }
|
76 | 92 |
|
77 | 93 | void output_function(
|
78 |
| - std::ostream &out, |
| 94 | + std::ostream &os, |
79 | 95 | const statement_list_parse_treet::functiont &function)
|
80 | 96 | {
|
81 |
| - output_tia_module_properties(function, out); |
82 |
| - output_return_value(function, out); |
83 |
| - output_common_var_declarations(out, function); |
84 |
| - output_network_list(out, function.networks); |
| 97 | + output_tia_module_properties(function, os); |
| 98 | + output_return_value(function, os); |
| 99 | + output_common_var_declarations(os, function); |
| 100 | + output_network_list(os, function.networks); |
85 | 101 | }
|
86 | 102 |
|
87 | 103 | void output_tia_module_properties(
|
88 | 104 | const statement_list_parse_treet::tia_modulet &module,
|
89 |
| - std::ostream &out) |
| 105 | + std::ostream &os) |
90 | 106 | {
|
91 |
| - out << "Name: " << module.name << '\n'; |
92 |
| - out << "Version: " << module.version << "\n\n"; |
| 107 | + os << "Name: " << module.name << '\n'; |
| 108 | + os << "Version: " << module.version << "\n\n"; |
93 | 109 | }
|
94 | 110 |
|
95 | 111 | void output_return_value(
|
96 | 112 | const statement_list_parse_treet::functiont &function,
|
97 |
| - std::ostream &out) |
| 113 | + std::ostream &os) |
98 | 114 | {
|
99 |
| - out << "Return type: "; |
| 115 | + os << "Return type: "; |
100 | 116 | if(function.return_type.is_nil())
|
101 |
| - out << "Void"; |
| 117 | + os << "Void"; |
102 | 118 | else
|
103 |
| - out << function.return_type.id(); |
104 |
| - out << "\n\n"; |
| 119 | + os << function.return_type.id(); |
| 120 | + os << "\n\n"; |
105 | 121 | }
|
106 | 122 |
|
107 | 123 | void output_common_var_declarations(
|
108 |
| - std::ostream &out, |
| 124 | + std::ostream &os, |
109 | 125 | const statement_list_parse_treet::tia_modulet &module)
|
110 | 126 | {
|
111 | 127 | if(!module.var_input.empty())
|
112 | 128 | {
|
113 |
| - out << "--------- Input Variables ----------\n\n"; |
114 |
| - output_var_declaration_list(out, module.var_input); |
| 129 | + os << "--------- Input Variables ----------\n\n"; |
| 130 | + output_var_declaration_list(os, module.var_input); |
115 | 131 | }
|
116 | 132 |
|
117 | 133 | if(!module.var_inout.empty())
|
118 | 134 | {
|
119 |
| - out << "--------- In/Out Variables ---------\n\n"; |
120 |
| - output_var_declaration_list(out, module.var_inout); |
| 135 | + os << "--------- In/Out Variables ---------\n\n"; |
| 136 | + output_var_declaration_list(os, module.var_inout); |
121 | 137 | }
|
122 | 138 |
|
123 | 139 | if(!module.var_output.empty())
|
124 | 140 | {
|
125 |
| - out << "--------- Output Variables ---------\n\n"; |
126 |
| - output_var_declaration_list(out, module.var_output); |
| 141 | + os << "--------- Output Variables ---------\n\n"; |
| 142 | + output_var_declaration_list(os, module.var_output); |
127 | 143 | }
|
128 | 144 |
|
129 | 145 | if(!module.var_constant.empty())
|
130 | 146 | {
|
131 |
| - out << "-------- Constant Variables --------\n\n"; |
132 |
| - output_var_declaration_list(out, module.var_constant); |
| 147 | + os << "-------- Constant Variables --------\n\n"; |
| 148 | + output_var_declaration_list(os, module.var_constant); |
133 | 149 | }
|
134 | 150 |
|
135 | 151 | if(!module.var_temp.empty())
|
136 | 152 | {
|
137 |
| - out << "---------- Temp Variables ----------\n\n"; |
138 |
| - output_var_declaration_list(out, module.var_temp); |
| 153 | + os << "---------- Temp Variables ----------\n\n"; |
| 154 | + output_var_declaration_list(os, module.var_temp); |
139 | 155 | }
|
140 | 156 | }
|
141 | 157 |
|
142 | 158 | void output_static_var_declarations(
|
143 |
| - std::ostream &out, |
| 159 | + std::ostream &os, |
144 | 160 | const statement_list_parse_treet::function_blockt &block)
|
145 | 161 | {
|
146 | 162 | if(!block.var_static.empty())
|
147 | 163 | {
|
148 |
| - out << "--------- Static Variables ---------\n\n"; |
149 |
| - output_var_declaration_list(out, block.var_static); |
| 164 | + os << "--------- Static Variables ---------\n\n"; |
| 165 | + output_var_declaration_list(os, block.var_static); |
150 | 166 | }
|
151 | 167 | }
|
152 | 168 |
|
153 | 169 | void output_var_declaration_list(
|
154 |
| - std::ostream &out, |
| 170 | + std::ostream &os, |
155 | 171 | const statement_list_parse_treet::var_declarationst &declarations)
|
156 | 172 | {
|
157 | 173 | for(const auto &declaration : declarations)
|
158 | 174 | {
|
159 |
| - output_var_declaration(out, declaration); |
160 |
| - out << "\n\n"; |
| 175 | + output_var_declaration(os, declaration); |
| 176 | + os << "\n\n"; |
161 | 177 | }
|
162 | 178 | }
|
163 | 179 |
|
164 | 180 | void output_var_declaration(
|
165 |
| - std::ostream &out, |
| 181 | + std::ostream &os, |
166 | 182 | const statement_list_parse_treet::var_declarationt &declaration)
|
167 | 183 | {
|
168 |
| - out << declaration.variable.pretty() << '\n'; |
169 |
| - out << " * default_value: "; |
| 184 | + os << declaration.variable.pretty() << '\n'; |
| 185 | + os << " * default_value: "; |
170 | 186 | if(declaration.default_value)
|
171 | 187 | {
|
172 | 188 | const constant_exprt &constant =
|
173 | 189 | to_constant_expr(declaration.default_value.value());
|
174 |
| - output_constant(out, constant); |
| 190 | + output_constant(os, constant); |
175 | 191 | }
|
176 | 192 | else
|
177 |
| - out << NO_VALUE; |
| 193 | + os << NO_VALUE; |
178 | 194 | }
|
179 | 195 |
|
180 | 196 | void output_network_list(
|
181 |
| - std::ostream &out, |
| 197 | + std::ostream &os, |
182 | 198 | const statement_list_parse_treet::networkst &networks)
|
183 | 199 | {
|
184 |
| - out << "-------------- Networks --------------\n\n"; |
| 200 | + os << "-------------- Networks --------------\n\n"; |
185 | 201 | for(const auto &network : networks)
|
186 | 202 | {
|
187 |
| - output_network(out, network); |
188 |
| - out << '\n'; |
| 203 | + output_network(os, network); |
| 204 | + os << '\n'; |
189 | 205 | }
|
190 | 206 | }
|
191 | 207 |
|
192 | 208 | void output_network(
|
193 |
| - std::ostream &out, |
| 209 | + std::ostream &os, |
194 | 210 | const statement_list_parse_treet::networkt &network)
|
195 | 211 | {
|
196 |
| - out << "Title: " << network.title.value_or(NO_VALUE) << '\n'; |
197 |
| - out << "Instructions: "; |
| 212 | + os << "Title: " << network.title.value_or(NO_VALUE) << '\n'; |
| 213 | + os << "Instructions: "; |
198 | 214 | if(network.instructions.empty())
|
199 |
| - out << NO_VALUE; |
200 |
| - out << '\n'; |
| 215 | + os << NO_VALUE; |
| 216 | + os << '\n'; |
201 | 217 | for(const auto &instruction : network.instructions)
|
202 | 218 | {
|
203 |
| - output_instruction(out, instruction); |
204 |
| - out << '\n'; |
| 219 | + output_instruction(os, instruction); |
| 220 | + os << '\n'; |
205 | 221 | }
|
206 | 222 | }
|
207 | 223 |
|
208 | 224 | void output_instruction(
|
209 |
| - std::ostream &out, |
| 225 | + std::ostream &os, |
210 | 226 | const statement_list_parse_treet::instructiont &instruction)
|
211 | 227 | {
|
212 | 228 | for(const codet &token : instruction.tokens)
|
213 | 229 | {
|
214 |
| - out << token.get_statement(); |
| 230 | + os << token.get_statement(); |
215 | 231 | for(const auto &expr : token.operands())
|
216 | 232 | {
|
217 |
| - if(expr.id() == ID_symbol) |
| 233 | + const symbol_exprt *const symbol = |
| 234 | + expr_try_dynamic_cast<symbol_exprt>(expr); |
| 235 | + if(symbol) |
218 | 236 | {
|
219 |
| - out << '\t' << expr.get(ID_identifier); |
| 237 | + os << '\t' << symbol->get_identifier(); |
220 | 238 | continue;
|
221 | 239 | }
|
222 | 240 | const constant_exprt *const constant =
|
223 | 241 | expr_try_dynamic_cast<constant_exprt>(expr);
|
224 | 242 | if(constant)
|
225 | 243 | {
|
226 |
| - out << '\t'; |
227 |
| - output_constant(out, *constant); |
| 244 | + os << '\t'; |
| 245 | + output_constant(os, *constant); |
228 | 246 | continue;
|
229 | 247 | }
|
230 |
| - const equal_exprt *const eq = expr_try_dynamic_cast<equal_exprt>(expr); |
231 |
| - if(eq) |
| 248 | + const equal_exprt *const equal = expr_try_dynamic_cast<equal_exprt>(expr); |
| 249 | + if(equal) |
232 | 250 | {
|
233 |
| - out << "\n\t" << eq->lhs().get(ID_identifier) |
234 |
| - << " := " << eq->rhs().get(ID_identifier); |
| 251 | + os << "\n\t"; |
| 252 | + output_parameter_assignment(os, *equal); |
| 253 | + continue; |
235 | 254 | }
|
236 |
| - else |
237 |
| - out << '\t' << expr.id(); |
| 255 | + os << '\t' << expr.id(); |
238 | 256 | }
|
239 | 257 | }
|
240 | 258 | }
|
0 commit comments