|
| 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 | + |
| 11 | +#include <time.h> |
| 12 | +#include <assert.h> |
| 13 | + |
| 14 | +#include <arith_tools.h> |
| 15 | +#include <pointer_offset_size.h> |
| 16 | + |
| 17 | +#include <numbering.h> |
| 18 | + |
| 19 | +#include "vcd_goto_trace.h" |
| 20 | + |
| 21 | +/*******************************************************************\ |
| 22 | +
|
| 23 | +Function: output_vcd |
| 24 | +
|
| 25 | + Inputs: |
| 26 | +
|
| 27 | + Outputs: |
| 28 | +
|
| 29 | + Purpose: |
| 30 | +
|
| 31 | +\*******************************************************************/ |
| 32 | + |
| 33 | +std::string as_vcd_binary( |
| 34 | + const exprt &expr, |
| 35 | + const namespacet &ns) |
| 36 | +{ |
| 37 | + const typet &type=ns.follow(expr.type()); |
| 38 | + |
| 39 | + if(expr.id()==ID_constant) |
| 40 | + { |
| 41 | + if(type.id()==ID_unsignedbv || |
| 42 | + type.id()==ID_signedbv || |
| 43 | + type.id()==ID_bv || |
| 44 | + type.id()==ID_fixedbv || |
| 45 | + type.id()==ID_floatbv || |
| 46 | + type.id()==ID_pointer) |
| 47 | + return expr.get_string(ID_value); |
| 48 | + } |
| 49 | + else if(expr.id()==ID_array) |
| 50 | + { |
| 51 | + std::string result; |
| 52 | + |
| 53 | + forall_operands(it, expr) |
| 54 | + result+=as_vcd_binary(*it, ns); |
| 55 | + |
| 56 | + return result; |
| 57 | + } |
| 58 | + else if(expr.id()==ID_struct) |
| 59 | + { |
| 60 | + std::string result; |
| 61 | + |
| 62 | + forall_operands(it, expr) |
| 63 | + result+=as_vcd_binary(*it, ns); |
| 64 | + |
| 65 | + return result; |
| 66 | + } |
| 67 | + else if(expr.id()==ID_union) |
| 68 | + { |
| 69 | + assert(expr.operands().size()==1); |
| 70 | + return as_vcd_binary(expr.op0(), ns); |
| 71 | + } |
| 72 | + |
| 73 | + // build "xxx" |
| 74 | + |
| 75 | + mp_integer width; |
| 76 | + |
| 77 | + if(type.id()==ID_unsignedbv || |
| 78 | + type.id()==ID_signedbv || |
| 79 | + type.id()==ID_floatbv || |
| 80 | + type.id()==ID_fixedbv || |
| 81 | + type.id()==ID_pointer || |
| 82 | + type.id()==ID_bv) |
| 83 | + width=string2integer(type.get_string(ID_width)); |
| 84 | + else |
| 85 | + width=pointer_offset_size(ns, type)*8; |
| 86 | + |
| 87 | + if(width>=0) |
| 88 | + { |
| 89 | + std::string result; |
| 90 | + |
| 91 | + for(; width!=0; --width) |
| 92 | + result+='x'; |
| 93 | + |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + return ""; |
| 98 | +} |
| 99 | + |
| 100 | +/*******************************************************************\ |
| 101 | +
|
| 102 | +Function: output_vcd |
| 103 | +
|
| 104 | + Inputs: |
| 105 | +
|
| 106 | + Outputs: |
| 107 | +
|
| 108 | + Purpose: |
| 109 | +
|
| 110 | +\*******************************************************************/ |
| 111 | + |
| 112 | +void output_vcd( |
| 113 | + const namespacet &ns, |
| 114 | + const goto_tracet &goto_trace, |
| 115 | + std::ostream &out) |
| 116 | +{ |
| 117 | + time_t t; |
| 118 | + time(&t); |
| 119 | + out << "$date\n " << ctime(&t) << "$end" << std::endl; |
| 120 | + |
| 121 | + // this is pretty arbitrary |
| 122 | + out << "$timescale 1 ns $end" << std::endl; |
| 123 | + |
| 124 | + // we first collect all variables that are assigned |
| 125 | + |
| 126 | + numbering<irep_idt> n; |
| 127 | + |
| 128 | + for(goto_tracet::stepst::const_iterator |
| 129 | + it=goto_trace.steps.begin(); |
| 130 | + it!=goto_trace.steps.end(); |
| 131 | + it++) |
| 132 | + { |
| 133 | + if(it->is_assignment()) |
| 134 | + { |
| 135 | + exprt lhs=it->original_lhs.is_not_nil()?it->original_lhs:it->lhs; |
| 136 | + irep_idt identifier=lhs.get(ID_identifier); |
| 137 | + const typet &type=lhs.type(); |
| 138 | + |
| 139 | + unsigned number=n.number(identifier); |
| 140 | + |
| 141 | + mp_integer width; |
| 142 | + |
| 143 | + if(type.id()==ID_bool) |
| 144 | + width=1; |
| 145 | + else if(type.id()==ID_unsignedbv || |
| 146 | + type.id()==ID_signedbv || |
| 147 | + type.id()==ID_floatbv || |
| 148 | + type.id()==ID_fixedbv || |
| 149 | + type.id()==ID_pointer || |
| 150 | + type.id()==ID_bv) |
| 151 | + width=string2integer(type.get_string(ID_width)); |
| 152 | + else |
| 153 | + width=pointer_offset_size(ns, lhs.type())*8; |
| 154 | + |
| 155 | + if(width>=1) |
| 156 | + out << "$var reg " << width << " V" << number << " " |
| 157 | + << identifier << " $end" << std::endl; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // end of header |
| 162 | + out << "$enddefinitions $end" << std::endl; |
| 163 | + |
| 164 | + unsigned timestamp=0; |
| 165 | + |
| 166 | + for(goto_tracet::stepst::const_iterator |
| 167 | + it=goto_trace.steps.begin(); |
| 168 | + it!=goto_trace.steps.end(); |
| 169 | + it++) |
| 170 | + { |
| 171 | + switch(it->type) |
| 172 | + { |
| 173 | + case goto_trace_stept::ASSIGNMENT: |
| 174 | + { |
| 175 | + exprt lhs=it->original_lhs.is_not_nil()?it->original_lhs:it->lhs; |
| 176 | + irep_idt identifier=lhs.get(ID_identifier); |
| 177 | + const typet &type=lhs.type(); |
| 178 | + |
| 179 | + out << '#' << timestamp << std::endl; |
| 180 | + timestamp++; |
| 181 | + |
| 182 | + unsigned number=n.number(identifier); |
| 183 | + |
| 184 | + // booleans are special in VCD |
| 185 | + if(type.id()==ID_bool) |
| 186 | + { |
| 187 | + if(it->value.is_true()) |
| 188 | + out << "1" << "V" << number << std::endl; |
| 189 | + else if(it->value.is_false()) |
| 190 | + out << "0" << "V" << number << std::endl; |
| 191 | + else |
| 192 | + out << "x" << "V" << number << std::endl; |
| 193 | + } |
| 194 | + else |
| 195 | + { |
| 196 | + std::string binary=as_vcd_binary(it->value, ns); |
| 197 | + |
| 198 | + if(binary!="") |
| 199 | + out << "b" << binary << " V" << number << " " << std::endl; |
| 200 | + } |
| 201 | + } |
| 202 | + break; |
| 203 | + |
| 204 | + default:; |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments