CBMC
vcd_goto_trace.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Traces of GOTO Programs in VCD (Value Change Dump) Format
4 
5 Author: Daniel Kroening
6 
7 Date: June 2011
8 
9 \*******************************************************************/
10 
13 
14 #include "vcd_goto_trace.h"
15 
16 #include <ctime>
17 
18 #include <util/arith_tools.h>
19 #include <util/numbering.h>
21 
22 #include "goto_trace.h"
23 
24 std::string as_vcd_binary(
25  const exprt &expr,
26  const namespacet &ns)
27 {
28  const typet &type = expr.type();
29 
30  if(expr.is_constant())
31  {
32  if(type.id()==ID_unsignedbv ||
33  type.id()==ID_signedbv ||
34  type.id()==ID_bv ||
35  type.id()==ID_fixedbv ||
36  type.id()==ID_floatbv ||
37  type.id()==ID_pointer)
38  return expr.get_string(ID_value);
39  }
40  else if(expr.id()==ID_array)
41  {
42  std::string result;
43 
44  for(const auto &op : expr.operands())
45  result += as_vcd_binary(op, ns);
46 
47  return result;
48  }
49  else if(expr.id()==ID_struct)
50  {
51  std::string result;
52 
53  for(const auto &op : expr.operands())
54  result += as_vcd_binary(op, ns);
55 
56  return result;
57  }
58  else if(expr.id()==ID_union)
59  {
60  return as_vcd_binary(to_union_expr(expr).op(), ns);
61  }
62 
63  // build "xxx"
64 
65  const auto width = pointer_offset_bits(type, ns);
66 
67  if(width.has_value())
68  return std::string(numeric_cast_v<std::size_t>(*width), 'x');
69  else
70  return "";
71 }
72 
74  const namespacet &ns,
75  const goto_tracet &goto_trace,
76  std::ostream &out)
77 {
78  time_t t;
79  time(&t);
80  out << "$date\n " << ctime(&t) << "$end" << "\n";
81 
82  // this is pretty arbitrary
83  out << "$timescale 1 ns $end" << "\n";
84 
85  // we first collect all variables that are assigned
86 
88 
89  for(const auto &step : goto_trace.steps)
90  {
91  if(step.is_assignment())
92  {
93  auto lhs_object=step.get_lhs_object();
94  if(lhs_object.has_value())
95  {
96  irep_idt identifier=lhs_object->get_identifier();
97  const typet &type=lhs_object->type();
98 
99  const auto number=n.number(identifier);
100 
101  const auto width = pointer_offset_bits(type, ns);
102 
103  if(width.has_value() && (*width) >= 1)
104  out << "$var reg " << (*width) << " V" << number << " " << identifier
105  << " $end"
106  << "\n";
107  }
108  }
109  }
110 
111  // end of header
112  out << "$enddefinitions $end" << "\n";
113 
114  unsigned timestamp=0;
115 
116  for(const auto &step : goto_trace.steps)
117  {
118  if(step.is_assignment())
119  {
120  auto lhs_object = step.get_lhs_object();
121  if(lhs_object.has_value())
122  {
123  irep_idt identifier = lhs_object->get_identifier();
124  const typet &type = lhs_object->type();
125 
126  out << '#' << timestamp << "\n";
127  timestamp++;
128 
129  const auto number = n.number(identifier);
130 
131  // booleans are special in VCD
132  if(type.id() == ID_bool)
133  {
134  if(step.full_lhs_value.is_true())
135  out << "1"
136  << "V" << number << "\n";
137  else if(step.full_lhs_value.is_false())
138  out << "0"
139  << "V" << number << "\n";
140  else
141  out << "x"
142  << "V" << number << "\n";
143  }
144  else
145  {
146  std::string binary = as_vcd_binary(step.full_lhs_value, ns);
147 
148  if(!binary.empty())
149  out << "b" << binary << " V" << number << " "
150  << "\n";
151  }
152  }
153  }
154  }
155 }
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.h:212
operandst & operands()
Definition: expr.h:94
Trace of a GOTO program.
Definition: goto_trace.h:177
stepst steps
Definition: goto_trace.h:180
const irep_idt & id() const
Definition: irep.h:384
const std::string & get_string(const irep_idt &name) const
Definition: irep.h:397
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
number_type number(const key_type &a)
Definition: numbering.h:37
The type of an expression, extends irept.
Definition: type.h:29
Traces of GOTO Programs.
static std::string binary(const constant_exprt &src)
Definition: json_expr.cpp:187
std::optional< mp_integer > pointer_offset_bits(const typet &type, const namespacet &ns)
Pointer Logic.
const union_exprt & to_union_expr(const exprt &expr)
Cast an exprt to a union_exprt.
Definition: std_expr.h:1811
char * ctime(const time_t *clock)
Definition: time.c:208
time_t time(time_t *tloc)
Definition: time.c:12
std::string as_vcd_binary(const exprt &expr, const namespacet &ns)
void output_vcd(const namespacet &ns, const goto_tracet &goto_trace, std::ostream &out)
Traces of GOTO Programs in VCD (Value Change Dump) Format.