cprover
statement_list_parse_tree_io.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Statement List Language Parse Tree Output
4
5Author: Matthias Weiss, matthias.weiss@diffblue.com
6
7\*******************************************************************/
8
11
14
15#include <util/arith_tools.h>
17#include <util/ieee_float.h>
18
20#define NO_VALUE "(none)"
21
25static void output_constant(std::ostream &os, const constant_exprt &constant)
26{
27 mp_integer ivalue;
28 if(!to_integer(constant, ivalue))
29 os << ivalue;
30 else if(can_cast_type<floatbv_typet>(constant.type()))
31 {
33 real.from_expr(constant);
34 os << real.to_float();
35 }
36 else
37 os << constant.get_value();
38}
39
43static void
44output_parameter_assignment(std::ostream &os, const equal_exprt &assignment)
45{
46 os << assignment.lhs().get(ID_identifier) << " := ";
47 const constant_exprt *const constant =
48 expr_try_dynamic_cast<constant_exprt>(assignment.rhs());
49 if(constant)
50 output_constant(os, *constant);
51 else
52 os << assignment.rhs().get(ID_identifier);
53}
54
56 std::ostream &out,
57 const statement_list_parse_treet &parse_tree)
58{
59 for(const auto &function_block : parse_tree.function_blocks)
60 {
61 out << "============== Function Block ==============\n";
62 output_function_block(out, function_block);
63 out << '\n';
64 }
65
66 for(const auto &function : parse_tree.functions)
67 {
68 out << "================= Function =================\n";
69 output_function(out, function);
70 out << '\n';
71 }
72
73 if(!parse_tree.tags.empty())
74 {
75 out << "================= Tag List =================\n";
76 for(const auto &tag : parse_tree.tags)
77 {
78 out << tag.pretty();
79 out << '\n';
80 }
81 }
82}
83
85 std::ostream &os,
87{
88 output_tia_module_properties(function_block, os);
89 output_common_var_declarations(os, function_block);
90 output_static_var_declarations(os, function_block);
91 output_network_list(os, function_block.networks);
92}
93
95 std::ostream &os,
97{
98 output_tia_module_properties(function, os);
99 output_return_value(function, os);
100 output_common_var_declarations(os, function);
101 output_network_list(os, function.networks);
102}
103
106 std::ostream &os)
107{
108 os << "Name: " << module.name << '\n';
109 os << "Version: " << module.version << "\n\n";
110}
111
114 std::ostream &os)
115{
116 os << "Return type: ";
117 if(function.return_type.is_nil())
118 os << "Void";
119 else
120 os << function.return_type.id();
121 os << "\n\n";
122}
123
125 std::ostream &os,
127{
128 if(!module.var_input.empty())
129 {
130 os << "--------- Input Variables ----------\n\n";
132 }
133
134 if(!module.var_inout.empty())
135 {
136 os << "--------- In/Out Variables ---------\n\n";
138 }
139
140 if(!module.var_output.empty())
141 {
142 os << "--------- Output Variables ---------\n\n";
144 }
145
146 if(!module.var_constant.empty())
147 {
148 os << "-------- Constant Variables --------\n\n";
150 }
151
152 if(!module.var_temp.empty())
153 {
154 os << "---------- Temp Variables ----------\n\n";
156 }
157}
158
160 std::ostream &os,
162{
163 if(!block.var_static.empty())
164 {
165 os << "--------- Static Variables ---------\n\n";
167 }
168}
169
171 std::ostream &os,
173{
174 for(const auto &declaration : declarations)
175 {
176 output_var_declaration(os, declaration);
177 os << "\n\n";
178 }
179}
180
182 std::ostream &os,
184{
185 os << declaration.variable.pretty() << '\n';
186 os << " * default_value: ";
187 if(declaration.default_value)
188 {
189 const constant_exprt &constant =
190 to_constant_expr(declaration.default_value.value());
191 output_constant(os, constant);
192 }
193 else
194 os << NO_VALUE;
195}
196
198 std::ostream &os,
200{
201 os << "-------------- Networks --------------\n\n";
202 for(const auto &network : networks)
203 {
204 output_network(os, network);
205 os << '\n';
206 }
207}
208
210 std::ostream &os,
212{
213 os << "Title: " << network.title.value_or(NO_VALUE) << '\n';
214 os << "Instructions: ";
215 if(network.instructions.empty())
216 os << NO_VALUE;
217 os << '\n';
218 for(const auto &instruction : network.instructions)
219 {
220 output_instruction(os, instruction);
221 os << '\n';
222 }
223}
224
226 std::ostream &os,
228{
229 for(const codet &token : instruction.tokens)
230 {
231 os << token.get_statement();
232 for(const auto &expr : token.operands())
233 {
234 const symbol_exprt *const symbol =
235 expr_try_dynamic_cast<symbol_exprt>(expr);
236 if(symbol)
237 {
238 os << '\t' << symbol->get_identifier();
239 continue;
240 }
241 const constant_exprt *const constant =
242 expr_try_dynamic_cast<constant_exprt>(expr);
243 if(constant)
244 {
245 os << '\t';
246 output_constant(os, *constant);
247 continue;
248 }
249 const equal_exprt *const equal = expr_try_dynamic_cast<equal_exprt>(expr);
250 if(equal)
251 {
252 os << "\n\t";
253 output_parameter_assignment(os, *equal);
254 continue;
255 }
256 os << '\t' << expr.id();
257 }
258 }
259}
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
Definition: arith_tools.cpp:19
Pre-defined bitvector types.
bool can_cast_type< floatbv_typet >(const typet &type)
Check whether a reference to a typet is a floatbv_typet.
exprt & lhs()
Definition: std_expr.h:580
exprt & rhs()
Definition: std_expr.h:590
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:29
const irep_idt & get_statement() const
Definition: std_code_base.h:65
A constant literal expression.
Definition: std_expr.h:2807
const irep_idt & get_value() const
Definition: std_expr.h:2815
Equality.
Definition: std_expr.h:1225
typet & type()
Return the type of the expression.
Definition: expr.h:82
operandst & operands()
Definition: expr.h:92
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:495
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:45
const irep_idt & id() const
Definition: irep.h:396
bool is_nil() const
Definition: irep.h:376
Intermediate representation of a parsed Statement List file before converting it into a goto program.
std::list< var_declarationt > var_declarationst
functionst functions
List of functions this parse tree includes.
function_blockst function_blocks
List of function blocks this parse tree includes.
std::vector< symbol_exprt > tags
List of tags that were included in the source.
Expression to hold a symbol (variable)
Definition: std_expr.h:80
const irep_idt & get_identifier() const
Definition: std_expr.h:109
BigInt mp_integer
Definition: smt_terms.h:12
#define NO_VALUE
String to indicate that there is no value.
void output_var_declaration(std::ostream &os, const statement_list_parse_treet::var_declarationt &declaration)
Prints the given Statement List variable declaration in a human-readable form to the given output str...
void output_return_value(const statement_list_parse_treet::functiont &function, std::ostream &os)
Prints the return value of a function to the given output stream.
void output_instruction(std::ostream &os, const statement_list_parse_treet::instructiont &instruction)
Prints the given Statement List instruction in a human-readable form to the given output stream.
void output_var_declaration_list(std::ostream &os, const statement_list_parse_treet::var_declarationst &declarations)
Prints all variable declarations of the given list to the given output stream.
void output_parse_tree(std::ostream &out, const statement_list_parse_treet &parse_tree)
Prints the given Statement List parse tree in a human-readable form to the given output stream.
static void output_constant(std::ostream &os, const constant_exprt &constant)
Prints a constant to the given output stream.
void output_tia_module_properties(const statement_list_parse_treet::tia_modulet &module, std::ostream &os)
Prints the basic information about a TIA module to the given output stream.
void output_function(std::ostream &os, const statement_list_parse_treet::functiont &function)
Prints the given Statement List function in a human-readable form to the given output stream.
void output_common_var_declarations(std::ostream &os, const statement_list_parse_treet::tia_modulet &module)
Prints all variable declarations functions and function blocks have in common to the given output str...
void output_network_list(std::ostream &os, const statement_list_parse_treet::networkst &networks)
Prints the given network list in a human-readable form to the given output stream.
void output_network(std::ostream &os, const statement_list_parse_treet::networkt &network)
Prints the given Statement List network in a human-readable form to the given output stream.
void output_function_block(std::ostream &os, const statement_list_parse_treet::function_blockt &function_block)
Prints the given Statement List function block in a human-readable form to the given output stream.
void output_static_var_declarations(std::ostream &os, const statement_list_parse_treet::function_blockt &block)
Prints the static variable declarations of a function block to the given output stream.
static void output_parameter_assignment(std::ostream &os, const equal_exprt &assignment)
Prints the assignment of a module parameter to the given output stream.
Statement List Language Parse Tree Output.
floatbv_typet get_real_type()
Creates a new type that resembles the 'Real' type of the Siemens PLC languages.
Statement List Type Helper.
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:2840
Structure for a simple function block in Statement List.
var_declarationst var_static
FB-exclusive static variable declarations.
Structure for a simple function in Statement List.
const typet return_type
FC-exclusive return type.
Represents a regular Statement List instruction which consists out of one or more codet tokens.
std::vector< codet > tokens
Data structure for all tokens of the instruction.
Representation of a network in Siemens TIA.
Base element of all modules in the Totally Integrated Automation (TIA) portal by Siemens.
var_declarationst var_constant
Constant variable declarations.
var_declarationst var_input
Input variable declarations.
const std::string version
Version of the module.
const irep_idt name
Name of the module.
var_declarationst var_inout
Inout variable declarations.
networkst networks
List of all networks of this module.
var_declarationst var_temp
Temp variable declarations.
var_declarationst var_output
Output variable declarations.
Struct for a single variable declaration in Statement List.
symbol_exprt variable
Representation of the variable, including identifier and type.
optionalt< exprt > default_value
Optional default value of the variable.