-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathstatement_list_parser.cpp
More file actions
363 lines (322 loc) · 11.8 KB
/
Copy pathstatement_list_parser.cpp
File metadata and controls
363 lines (322 loc) · 11.8 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*******************************************************************\
Module: Statement List Language Parser
Author: Matthias Weiss, matthias.weiss@diffblue.com
\*******************************************************************/
/// \file
/// Statement List Language Parser
#include "statement_list_parser.h"
#include <util/std_code.h>
#include <util/string_constant.h>
#include "statement_list_parse_tree.h"
#include "statement_list_parse_tree_io.h"
#include <algorithm>
#include <iostream>
#include <iterator>
int statement_list_parsert::instance_count = 0;
/// Searches for the name of the TIA module inside of its root
/// expression.
/// \param root: Expression that includes the element's name as a
/// direct operand.
/// \return The name of the function element.
static irep_idt find_name(const exprt &root)
{
for(const exprt &op : root.operands())
{
if(op.get(ID_statement_list_type) == ID_statement_list_identifier)
return op.get(ID_value);
}
UNREACHABLE; // Root expression should always have a name
}
void statement_list_parsert::add_tag_list(const exprt &tag_list)
{
const exprt::operandst &ops = tag_list.operands();
transform(
begin(ops),
end(ops),
std::back_inserter(parse_tree.tags),
static_cast<const symbol_exprt &(*)(const exprt &)>(to_symbol_expr));
}
/// Searches for the version of the TIA module inside of its root
/// expression.
/// \param root: Expression that includes the element's version as a
/// direct operand.
/// \return The version of the element.
static std::string find_version(const exprt &root)
{
for(const exprt &op : root.operands())
{
if(op.get(ID_statement_list_type) == ID_statement_list_version)
{
const string_constantt &constant{to_string_constant(op)};
return id2string(constant.value());
}
}
UNREACHABLE; // Root expression should always have a version
}
/// Searches for the return type of a function inside of its root
/// expression.
/// \param root: Expression that includes the function's return type as a
/// direct operand.
/// \return The return type of the function.
static typet find_return_value(const exprt &root)
{
INVARIANT(
root.id() == ID_statement_list_function,
"Expression ID should be statement_list_function");
for(const exprt &op : root.operands())
{
if(op.get(ID_statement_list_type) == ID_statement_list_return)
return op.type();
}
UNREACHABLE; // Root expression of FC should always have a return value
}
/// Searches for the variable list of the TIA module inside of its root
/// expression.
/// \param root: Expression that includes the element's variable list as a
/// direct operand.
/// \return The variable list of the element.
static exprt find_variable_list(const exprt &root)
{
for(const exprt &op : root.operands())
{
if(op.id() == ID_statement_list_var_decls)
return op;
}
UNREACHABLE; // Root expression should always have a variable list
}
/// Adds all variable declarations (which can have a default value) to the
/// given list.
/// \param parse_tree_list: The list to fill with all declarations.
/// \param var_list: The root expression of a variable list with optional
/// default values.
static void fill_vars_with_default_values(
statement_list_parse_treet::var_declarationst &parse_tree_list,
const exprt &var_list)
{
for(const exprt &entry : var_list.operands())
{
std::vector<symbol_exprt> symbols;
exprt default_value = nil_exprt();
for(const exprt &part : entry.operands())
{
const symbol_exprt *const symbol =
expr_try_dynamic_cast<symbol_exprt>(part);
if(symbol)
symbols.push_back(*symbol);
else
default_value = part;
}
for(const symbol_exprt &symbol : symbols)
{
statement_list_parse_treet::var_declarationt declaration{symbol};
if(default_value.is_not_nil())
declaration.default_value = default_value;
parse_tree_list.push_back(declaration);
}
}
}
/// Adds all temp variable declarations (variable declarations which can't have
/// a default value) to the given list.
/// \param parse_tree_list: The list to fill with all declarations.
/// \param temp_vars: The root expression of a temp variable list.
static void fill_temp_vars(
statement_list_parse_treet::var_declarationst &parse_tree_list,
const exprt &temp_vars)
{
for(const exprt &entry : temp_vars.operands())
{
for(const exprt &part : entry.operands())
{
const symbol_exprt *const symbol =
expr_try_dynamic_cast<symbol_exprt>(part);
if(symbol)
{
statement_list_parse_treet::var_declarationt declaration{*symbol};
parse_tree_list.push_back(declaration);
}
else
UNREACHABLE; // Temp variables should not have an initial value.
}
}
}
/// Adds all valid variable declarations to the given function.
/// \param function: The TIA element to which the variables belong.
/// \param var_decls: The root expression of a valid variable list.
static void find_variables(
statement_list_parse_treet::functiont &function,
const exprt &var_decls)
{
for(const exprt &decls : var_decls.operands())
{
if(decls.id() == ID_statement_list_var_input)
fill_vars_with_default_values(function.var_input, decls);
else if(decls.id() == ID_statement_list_var_inout)
fill_vars_with_default_values(function.var_inout, decls);
else if(decls.id() == ID_statement_list_var_output)
fill_vars_with_default_values(function.var_output, decls);
else if(decls.id() == ID_statement_list_var_constant)
fill_vars_with_default_values(function.var_constant, decls);
else if(decls.id() == ID_statement_list_var_temp)
fill_temp_vars(function.var_temp, decls);
}
}
/// Adds all valid variable declarations to the given function block.
/// \param block: The TIA element to which the variables belong.
/// \param var_decls: The root expression of a valid variable list.
static void find_variables(
statement_list_parse_treet::function_blockt &block,
const exprt &var_decls)
{
for(const exprt &decls : var_decls.operands())
{
if(ID_statement_list_var_input == decls.id())
fill_vars_with_default_values(block.var_input, decls);
else if(ID_statement_list_var_inout == decls.id())
fill_vars_with_default_values(block.var_inout, decls);
else if(ID_statement_list_var_output == decls.id())
fill_vars_with_default_values(block.var_output, decls);
else if(ID_statement_list_var_static == decls.id())
fill_vars_with_default_values(block.var_static, decls);
else if(ID_statement_list_var_constant == decls.id())
fill_vars_with_default_values(block.var_constant, decls);
else if(ID_statement_list_var_temp == decls.id())
fill_temp_vars(block.var_temp, decls);
}
}
/// Searches for the network list of the TIA element inside of its root
/// expression.
/// \param root: Expression that includes the element's network list as a
/// direct operand.
/// \return The network list of the element.
static exprt find_network_list(const exprt &root)
{
for(const exprt &op : root.operands())
{
if(op.id() == ID_statement_list_networks)
return op;
}
UNREACHABLE; // Root expression should always have a network list
}
/// Searches for the title of a network inside of its root expression.
/// \param network: Expression that includes the network's title as a
/// direct operand.
/// \return The title of the network.
static std::string find_network_title(const exprt &network)
{
for(const exprt &network_element : network.operands())
{
if(network_element.get(ID_statement_list_type) == ID_statement_list_title)
return network_element.get(ID_value).c_str();
}
UNREACHABLE; // Network expression should always have a title
}
/// Searches for the instruction list of a network inside of its root
/// expression.
/// \param network: Expression that includes the network's instructions as a
/// direct operand.
/// \return The instruction list expression of the network.
static exprt find_network_instructions(const exprt &network)
{
for(const exprt &network_element : network.operands())
{
if(network_element.id() == ID_statement_list_instructions)
return network_element;
}
UNREACHABLE; // Network expression should always have an instruction list
}
/// Adds all valid instructions to the given network.
/// \param network: The network to which the instructions belong.
/// \param instructions: The root expression of a valid instruction list.
static void find_instructions(
statement_list_parse_treet::networkt &network,
const exprt &instructions)
{
for(const exprt &instruction_expr : instructions.operands())
{
statement_list_parse_treet::instructiont instruction;
codet code_token(to_multi_ary_expr(instruction_expr).op0().id());
string_constantt label{ID_nil};
for(auto op_it = std::next(instruction_expr.operands().begin());
op_it != end(instruction_expr.operands());
++op_it)
{
if(op_it->get(ID_statement_list_type) == ID_label)
label = to_string_constant(*op_it);
else if(op_it->is_not_nil())
code_token.add_to_operands(*op_it);
}
if(label.value() == ID_nil)
instruction.add_token(code_token);
else
instruction.add_token(code_labelt{label.value(), code_token});
network.add_instruction(instruction);
}
}
/// Adds all valid networks and their instructions to the given function
/// element.
/// \param module: The TIA element to which the networks belong.
/// \param network_list: The root expression of a valid network list.
static void find_networks(
statement_list_parse_treet::tia_modulet &module,
const exprt &network_list)
{
for(const exprt &expr_network : network_list.operands())
{
const std::string title(find_network_title(expr_network));
statement_list_parse_treet::networkt network(title);
const exprt instructions = find_network_instructions(expr_network);
find_instructions(network, instructions);
module.add_network(network);
}
}
void statement_list_parsert::add_function_block(const exprt &block)
{
INVARIANT(
block.id() == ID_statement_list_function_block,
"Root expression ID should be ID_statement_list_function_block");
// Generate new function block.
statement_list_parse_treet::function_blockt fb{find_name(block),
find_version(block)};
// Fill the block with networks and variables.
find_variables(fb, find_variable_list(block));
find_networks(fb, find_network_list(block));
parse_tree.add_function_block(fb);
}
void statement_list_parsert::add_function(const exprt &function)
{
INVARIANT(
function.id() == ID_statement_list_function,
"Expression ID should be statement_list_function");
// Generate new function.
statement_list_parse_treet::functiont fn{
find_name(function), find_version(function), find_return_value(function)};
// Fill the function with networks and variables.
find_variables(fn, find_variable_list(function));
find_networks(fn, find_network_list(function));
parse_tree.add_function(fn);
}
int yystatement_listlex_init_extra(statement_list_parsert *, void **);
int yystatement_listlex_destroy(void *);
/// Defined in statement_list_y.tab.cpp. Main function for the parse process
/// generated by bison, performs all necessary steps to fill the parse tree.
int yystatement_listparse(statement_list_parsert &, void *);
void yystatement_listset_debug(int, void *);
bool statement_list_parsert::parse()
{
void *scanner;
yystatement_listlex_init_extra(this, &scanner);
#ifdef STATEMENT_LIST_DEBUG
yystatement_listset_debug(1, scanner);
#endif
bool parse_fail = yystatement_listparse(*this, scanner) != 0;
yystatement_listlex_destroy(scanner);
return parse_fail;
}
void statement_list_parsert::print_tree(std::ostream &out) const
{
output_parse_tree(out, parse_tree);
}
void statement_list_parsert::swap_tree(statement_list_parse_treet &other)
{
parse_tree.swap(other);
}