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