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