CBMC
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 
13 
14 #include <util/arith_tools.h>
15 #include <util/bitvector_types.h>
16 #include <util/ieee_float.h>
17 #include <util/std_code.h>
18 
20 
22 #define NO_VALUE "(none)"
23 
27 static void output_constant(std::ostream &os, const constant_exprt &constant)
28 {
29  mp_integer ivalue;
30  if(!to_integer(constant, ivalue))
31  os << ivalue;
32  else if(can_cast_type<floatbv_typet>(constant.type()))
33  {
34  ieee_floatt real{get_real_type()};
35  real.from_expr(constant);
36  os << real.to_float();
37  }
38  else
39  os << constant.get_value();
40 }
41 
46  std::ostream &os,
47  const code_frontend_assignt &assignment)
48 {
49  os << assignment.lhs().get(ID_identifier) << " := ";
50  const constant_exprt *const constant =
51  expr_try_dynamic_cast<constant_exprt>(assignment.rhs());
52  if(constant)
53  output_constant(os, *constant);
54  else
55  os << assignment.rhs().get(ID_identifier);
56 }
57 
59  std::ostream &out,
60  const statement_list_parse_treet &parse_tree)
61 {
62  for(const auto &function_block : parse_tree.function_blocks)
63  {
64  out << "============== Function Block ==============\n";
65  output_function_block(out, function_block);
66  out << '\n';
67  }
68 
69  for(const auto &function : parse_tree.functions)
70  {
71  out << "================= Function =================\n";
72  output_function(out, function);
73  out << '\n';
74  }
75 
76  if(!parse_tree.tags.empty())
77  {
78  out << "================= Tag List =================\n";
79  for(const auto &tag : parse_tree.tags)
80  {
81  out << tag.pretty();
82  out << '\n';
83  }
84  }
85 }
86 
88  std::ostream &os,
89  const statement_list_parse_treet::function_blockt &function_block)
90 {
91  output_tia_module_properties(function_block, os);
92  output_common_var_declarations(os, function_block);
93  output_static_var_declarations(os, function_block);
94  output_network_list(os, function_block.networks);
95 }
96 
98  std::ostream &os,
100 {
101  output_tia_module_properties(function, os);
102  output_return_value(function, os);
103  output_common_var_declarations(os, function);
104  output_network_list(os, function.networks);
105 }
106 
109  std::ostream &os)
110 {
111  os << "Name: " << module.name << '\n';
112  os << "Version: " << module.version << "\n\n";
113 }
114 
117  std::ostream &os)
118 {
119  os << "Return type: ";
120  if(function.return_type.is_nil())
121  os << "Void";
122  else
123  os << function.return_type.id();
124  os << "\n\n";
125 }
126 
128  std::ostream &os,
130 {
131  if(!module.var_input.empty())
132  {
133  os << "--------- Input Variables ----------\n\n";
135  }
136 
137  if(!module.var_inout.empty())
138  {
139  os << "--------- In/Out Variables ---------\n\n";
141  }
142 
143  if(!module.var_output.empty())
144  {
145  os << "--------- Output Variables ---------\n\n";
147  }
148 
149  if(!module.var_constant.empty())
150  {
151  os << "-------- Constant Variables --------\n\n";
153  }
154 
155  if(!module.var_temp.empty())
156  {
157  os << "---------- Temp Variables ----------\n\n";
159  }
160 }
161 
163  std::ostream &os,
165 {
166  if(!block.var_static.empty())
167  {
168  os << "--------- Static Variables ---------\n\n";
170  }
171 }
172 
174  std::ostream &os,
176 {
177  for(const auto &declaration : declarations)
178  {
179  output_var_declaration(os, declaration);
180  os << "\n\n";
181  }
182 }
183 
185  std::ostream &os,
187 {
188  os << declaration.variable.pretty() << '\n';
189  os << " * default_value: ";
190  if(declaration.default_value)
191  {
192  const constant_exprt &constant =
193  to_constant_expr(declaration.default_value.value());
194  output_constant(os, constant);
195  }
196  else
197  os << NO_VALUE;
198 }
199 
201  std::ostream &os,
203 {
204  os << "-------------- Networks --------------\n\n";
205  for(const auto &network : networks)
206  {
207  output_network(os, network);
208  os << '\n';
209  }
210 }
211 
213  std::ostream &os,
215 {
216  os << "Title: " << network.title.value_or(NO_VALUE) << '\n';
217  os << "Instructions: ";
218  if(network.instructions.empty())
219  os << NO_VALUE;
220  os << '\n';
221  for(const auto &instruction : network.instructions)
222  {
223  output_instruction(os, instruction);
224  os << '\n';
225  }
226 }
227 
229  std::ostream &os,
230  const statement_list_parse_treet::instructiont &instruction)
231 {
232  for(const codet &token : instruction.tokens)
233  {
234  os << token.get_statement();
235  for(const auto &expr : token.operands())
236  {
237  const symbol_exprt *const symbol =
238  expr_try_dynamic_cast<symbol_exprt>(expr);
239  if(symbol)
240  {
241  os << '\t' << symbol->get_identifier();
242  continue;
243  }
244  const constant_exprt *const constant =
245  expr_try_dynamic_cast<constant_exprt>(expr);
246  if(constant)
247  {
248  os << '\t';
249  output_constant(os, *constant);
250  continue;
251  }
252  if(const auto assign = expr_try_dynamic_cast<code_frontend_assignt>(expr))
253  {
254  os << "\n\t";
255  output_parameter_assignment(os, *assign);
256  continue;
257  }
258  os << '\t' << expr.id();
259  }
260  }
261 }
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:20
Pre-defined bitvector types.
bool can_cast_type< floatbv_typet >(const typet &type)
Check whether a reference to a typet is a floatbv_typet.
A codet representing an assignment in the program.
Definition: std_code.h:24
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:3000
const irep_idt & get_value() const
Definition: std_expr.h:3008
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:482
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:44
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:131
const irep_idt & get_identifier() const
Definition: std_expr.h:160
BigInt mp_integer
Definition: smt_terms.h:17
#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.
static void output_parameter_assignment(std::ostream &os, const code_frontend_assignt &assignment)
Prints the assignment of a module parameter 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.
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:3055
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.
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.
std::optional< exprt > default_value
Optional default value of the variable.
symbol_exprt variable
Representation of the variable, including identifier and type.