Skip to content

Commit e0e293b

Browse files
Merge pull request #5425 from thomasspriggs/tas/bit_vector_binary
Add binary representation to all bitvector values in the xml trace
2 parents a82c7d0 + d744e2d commit e0e293b

File tree

6 files changed

+82
-23
lines changed

6 files changed

+82
-23
lines changed

doc/assets/xml_spec.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ always present
169169
- `full_lhs`: original lhs expression (after dereferencing)
170170

171171
- `full_lhs_value`: a constant with the new value.
172-
- if the type is a floating point number type, there will be an attribute`binary`
173-
showing its value.
172+
- If the type of data can be represented as a fixed width sequence of bits
173+
then, there will be an attribute `binary` containing the binary
174+
representation. If the data type is signed and the value is negative
175+
then the binary will be encoded using two's complement.
174176

175177
**Example**:
176178

@@ -181,7 +183,7 @@ always present
181183
<location .. />
182184
<type>signed int</type>
183185
<full_lhs>b</full_lhs>
184-
<full_lhs_value>-1879048192</full_lhs_value>
186+
<full_lhs_value binary="10010000000000000000000000000000">-1879048192</full_lhs_value>
185187
</assignment>
186188
<assignment assignment_type="state" base_name="d" display_name="main::1::d" hidden="false" identifier="main::1::d" mode="C" step_nr="26" thread="0">
187189
<location file="main.i" function="main" line="3" working-directory="/home/tkiley/workspace/cbmc/regression/cbmc/Float24"/>

doc/assets/xml_spec.tex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ \subsection{Trace Steps in XML}
149149
\item \texttt{full\_lhs}: original lhs expression (after dereferencing)
150150
\item \texttt{full\_lhs\_value}: a constant with the new value.
151151
\begin{itemize}
152-
\item if the type is a floating point number type, there will be an attribute
153-
\texttt{binary} showing its value.
152+
\item If the type of data can be represented as a fixed width sequence of bits
153+
then, there will be an attribute \texttt{`binary`} containing the binary
154+
representation. If the data type is signed and the value is negative
155+
then the binary will be encoded using two's complement.
154156
\end{itemize}
155157
156158
\end{itemize}
@@ -164,7 +166,7 @@ \subsection{Trace Steps in XML}
164166
<location .. />
165167
<type>signed int</type>
166168
<full_lhs>b</full_lhs>
167-
<full_lhs_value>-1879048192</full_lhs_value>
169+
<full_lhs_value binary="10010000000000000000000000000000">-1879048192</full_lhs_value>
168170
</assignment>
169171
<assignment assignment_type="state" base_name="d" display_name="main::1::d" hidden="false" identifier="main::1::d" mode="C" step_nr="26" thread="0">
170172
<location ../>

regression/cbmc/integral-trace/test.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <assert.h>
2+
#include <inttypes.h>
3+
#include <math.h>
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
#include <stdlib.h>
7+
8+
int main()
9+
{
10+
int8_t i1 = 2;
11+
int16_t i2 = 3;
12+
int32_t i3 = 4;
13+
int64_t i4 = 5;
14+
15+
int8_t in1 = -2;
16+
int16_t in2 = -3;
17+
int32_t in3 = -4;
18+
int64_t in4 = -5;
19+
20+
uint8_t u8 = 'a';
21+
uint16_t u16 = 8;
22+
uint32_t u32 = 16;
23+
uint64_t u64 = 32;
24+
25+
void *ptr = malloc(1);
26+
27+
assert(false);
28+
29+
return 0;
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CORE
2+
test.c
3+
--trace --xml-ui
4+
activate-multi-line-match
5+
^EXIT=10$
6+
^SIGNAL=0$
7+
VERIFICATION FAILED
8+
<full_lhs>u8</full_lhs>\s*<full_lhs_value binary="01100001">97</full_lhs_value>
9+
<full_lhs>u16</full_lhs>\s*<full_lhs_value binary="0{12}1000">8</full_lhs_value>
10+
<full_lhs>u32</full_lhs>\s*<full_lhs_value binary="0{27}10000">16ul?</full_lhs_value>
11+
<full_lhs>u64</full_lhs>\s*<full_lhs_value binary="0{58}100000">32ull?</full_lhs_value>
12+
<full_lhs>i1</full_lhs>\s*<full_lhs_value binary="0{6}10">2</full_lhs_value>
13+
<full_lhs>i2</full_lhs>\s*<full_lhs_value binary="0{14}11">3</full_lhs_value>
14+
<full_lhs>i3</full_lhs>\s*<full_lhs_value binary="0{29}100">4</full_lhs_value>
15+
<full_lhs>i4</full_lhs>\s*<full_lhs_value binary="0{61}101">5ll?</full_lhs_value>
16+
<full_lhs>in1</full_lhs>\s*<full_lhs_value binary="1{6}10">-2</full_lhs_value>
17+
<full_lhs>in2</full_lhs>\s*<full_lhs_value binary="1{14}01">-3</full_lhs_value>
18+
<full_lhs>in3</full_lhs>\s*<full_lhs_value binary="1{29}100">-4</full_lhs_value>
19+
<full_lhs>in4</full_lhs>\s*<full_lhs_value binary="1{61}011">-5ll?</full_lhs_value>
20+
--
21+
--
22+
Checks that the binary value is printed for integral types.

regression/cbmc/xml-trace/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ VERIFICATION FAILED
88
<location file=".*" function="test" line="\d+" working-directory=".*"/>
99
<type>union myunion</type>
1010
<full_lhs>byte_extract_little_endian\(u, 0ll?, .*int.*\)</full_lhs>
11-
<full_lhs_value>\d+ll?</full_lhs_value>
11+
<full_lhs_value binary="[01]+">\d+ll?</full_lhs_value>
1212
<value>\{ \.i=\d+ll? \}</value>
1313
<value_expression>
1414
<union>

src/goto-programs/xml_goto_trace.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,34 @@ Author: Daniel Kroening
2525
#include "structured_trace_util.h"
2626
#include "xml_expr.h"
2727

28-
bool full_lhs_value_includes_binary(
29-
const goto_trace_stept &step,
30-
const namespacet &ns)
31-
{
32-
return can_cast_type<floatbv_typet>(step.full_lhs_value.type());
33-
}
34-
3528
xmlt full_lhs_value(const goto_trace_stept &step, const namespacet &ns)
3629
{
37-
xmlt full_lhs_value{"full_lhs_value"};
30+
xmlt value_xml{"full_lhs_value"};
31+
32+
const exprt &value = step.full_lhs_value;
33+
if(value.is_nil())
34+
return value_xml;
3835

3936
const auto &lhs_object = step.get_lhs_object();
4037
const irep_idt identifier =
4138
lhs_object.has_value() ? lhs_object->get_identifier() : irep_idt();
39+
value_xml.data = from_expr(ns, identifier, value);
4240

43-
if(step.full_lhs_value.is_not_nil())
44-
full_lhs_value.data = from_expr(ns, identifier, step.full_lhs_value);
45-
if(full_lhs_value_includes_binary(step, ns))
41+
const auto &bv_type = type_try_dynamic_cast<bitvector_typet>(value.type());
42+
const auto &constant = expr_try_dynamic_cast<constant_exprt>(value);
43+
if(bv_type && constant)
4644
{
47-
const auto width = to_floatbv_type(step.full_lhs_value.type()).get_width();
48-
const auto binary_representation = integer2binary(
49-
bvrep2integer(step.full_lhs_value.get(ID_value), width, false), width);
50-
full_lhs_value.set_attribute("binary", binary_representation);
45+
const auto width = bv_type->get_width();
46+
// It is fine to pass `false` into the `is_signed` parameter, even for
47+
// signed values, because the subsequent conversion to binary will result
48+
// in the same value either way. E.g. if the value is `-1` for a signed 8
49+
// bit value, this will convert to `255` which is `11111111` in binary,
50+
// which is the desired result.
51+
const auto binary_representation =
52+
integer2binary(bvrep2integer(constant->get_value(), width, false), width);
53+
value_xml.set_attribute("binary", binary_representation);
5154
}
52-
return full_lhs_value;
55+
return value_xml;
5356
}
5457

5558
void convert(

0 commit comments

Comments
 (0)