Skip to content

Commit 768eafc

Browse files
author
Thomas Kiley
authored
Merge pull request #5322 from thk123/print-binary-values-of-floats
Print binary values of floats
2 parents 319a06c + be6f20f commit 768eafc

File tree

9 files changed

+174
-16
lines changed

9 files changed

+174
-16
lines changed

doc/assets/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
This folder contains resources for the documentation.
2+
3+
# XML Spec
4+
5+
The folllowing files relate to the specification of the XML trace output when using the `--xml-ui` flag.
6+
7+
## xml_spec.xsd
8+
The`xml_spec.xsd` file is an XSD specification of the trace. You can validate an
9+
output xml file from CBMC using the following invocation:
10+
11+
```bash
12+
$ cbmc --xml-ui main.c | \
13+
xmllint --schema xml_spec.xsd --xpath "cprover/result/goto_trace" -
14+
```
15+
16+
## xml_spec.tex
17+
18+
This is a LaTeX document describing the specification of the trace. It can be compiled to pdf using:
19+
20+
```bash
21+
$ pdflatex -shell-escape xml_spec.tex
22+
```
23+
24+
This requires:
25+
26+
- texlive-latex-base
27+
- texlive-latex-extra (for minted package)

doc/assets/xml_spec.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ always present
168168

169169
- `full_lhs`: original lhs expression (after dereferencing)
170170

171-
- `full_lhs_value`: a constant with the new value
171+
- `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.
172174

173175
**Example**:
174176

@@ -181,6 +183,12 @@ always present
181183
<full_lhs>b</full_lhs>
182184
<full_lhs_value>-1879048192</full_lhs_value>
183185
</assignment>
186+
<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">
187+
<location file="main.i" function="main" line="3" working-directory="/home/tkiley/workspace/cbmc/regression/cbmc/Float24"/>
188+
<type>double</type>
189+
<full_lhs>d</full_lhs>
190+
<full_lhs_value binary="0100000000010100011001100110011001100110011001100110011001100110">5.1</full_lhs_value>
191+
</assignment>
184192
```
185193

186194
**XSD**:
@@ -192,7 +200,15 @@ always present
192200
<xs:element name="location" minOccurs="0"></xs:element>
193201
<xs:element name="type" type="xs:string" minOccurs="0"></xs:element>
194202
<xs:element name="full_lhs" type="xs:string"></xs:element>
195-
<xs:element name="full_lhs_value" type="xs:int"></xs:element>
203+
<xs:element name="full_lhs_value">
204+
<xs:complexType>
205+
<xs:simpleContent>
206+
<xs:extension base="xs:string">
207+
<xs:attribute name="binary" type="xs:string"/>
208+
</xs:extension>
209+
</xs:simpleContent>
210+
</xs:complexType>
211+
</xs:element>
196212
</xs:all>
197213
<xs:attributeGroup ref="traceStepAttrs">
198214
<xs:attribute name="assignment_type" type="xs:string"></xs:attribute>

doc/assets/xml_spec.tex

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,15 @@ \subsection{Trace Steps in XML}
147147
always present
148148
\begin{itemize}
149149
\item \texttt{full\_lhs}: original lhs expression (after dereferencing)
150-
\item \texttt{full\_lhs\_value}: a constant with the new value
150+
\item \texttt{full\_lhs\_value}: a constant with the new value.
151+
\begin{itemize}
152+
\item if the type is a floating point number type, there will be an attribute
153+
\texttt{binary} showing its value.
154+
\end{itemize}
155+
151156
\end{itemize}
152157
158+
153159
\noindent\textbf{Example}:
154160
\begin{minted}{xml}
155161
<assignment assignment_type="state" base_name="b" display_name="main::1::b"
@@ -160,6 +166,13 @@ \subsection{Trace Steps in XML}
160166
<full_lhs>b</full_lhs>
161167
<full_lhs_value>-1879048192</full_lhs_value>
162168
</assignment>
169+
<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">
170+
<location ../>
171+
<type>double</type>
172+
<full_lhs>d</full_lhs>
173+
<full_lhs_value
174+
binary="0100000000010100011001100110011001100110011001100110011001100110">5.1</full_lhs_value>
175+
</assignment>
163176
\end{minted}
164177
165178
\noindent\textbf{XSD}:
@@ -170,7 +183,15 @@ \subsection{Trace Steps in XML}
170183
<xs:element name="location" minOccurs="0"></xs:element>
171184
<xs:element name="type" type="xs:string" minOccurs="0"></xs:element>
172185
<xs:element name="full_lhs" type="xs:string"></xs:element>
173-
<xs:element name="full_lhs_value" type="xs:int"></xs:element>
186+
<xs:element name="full_lhs_value">
187+
<xs:complexType>
188+
<xs:simpleContent>
189+
<xs:extension base="xs:string">
190+
<xs:attribute name="binary" type="xs:string"/>
191+
</xs:extension>
192+
</xs:simpleContent>
193+
</xs:complexType>
194+
</xs:element>
174195
</xs:all>
175196
<xs:attributeGroup ref="traceStepAttrs">
176197
<xs:attribute name="assignment_type" type="xs:string"></xs:attribute>

doc/assets/xml_spec.xsd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@
4040
<xs:element ref="location" minOccurs="0"/>
4141
<xs:element name="type" type="xs:string" minOccurs="0"/>
4242
<xs:element name="full_lhs" type="xs:string"/>
43-
<xs:element name="full_lhs_value" type="xs:string"/>
43+
<xs:element name="full_lhs_value">
44+
<xs:complexType>
45+
<xs:simpleContent>
46+
<xs:extension base="xs:string">
47+
<xs:attribute name="binary" type="xs:string"/>
48+
</xs:extension>
49+
</xs:simpleContent>
50+
</xs:complexType>
51+
</xs:element>
4452
</xs:all>
4553
<xs:attributeGroup ref="traceStepAttrs"/>
4654
<xs:attribute name="assignment_type" type="xs:string"/>

doc/cprover-manual/cbmc-tutorial.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ possible output format.
138138
```
139139

140140
The specification of the XML trace output can be found here: [XML
141-
Specification](https://raw.githubusercontent.com/diffblue/cbmc/develop/doc/assets/xml_spec.tex)
142-
and can be build by `pdflatex -shell-escape xml_spec.tex`. Alternatively, you
143-
view it in Markdown [here](../../../assets/xml_spec).
141+
Specification](../assets/xml_spec.tex)
142+
and can be built by `pdflatex -shell-escape xml_spec.tex`. See the [README
143+
](../assets/README.md#xml_spec.tex) for details.
144+
145+
Alternatively, you can view it in [Markdown](../assets/xml_spec.md).
144146

145147
### Verifying Modules
146148

@@ -348,4 +350,3 @@ comes with a small set of definitions, which includes functions such as
348350

349351
We also have a [list of interesting applications of
350352
CBMC](http://www.cprover.org/cbmc/applications/).
351-

regression/cbmc/Float24/test.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ main.i
44
^EXIT=10$
55
^SIGNAL=0$
66
VERIFICATION FAILED
7-
<full_lhs_value>5\.1</full_lhs_value>
7+
<full_lhs_value binary="[01]+">5\.1</full_lhs_value>
88
--
99
^warning: ignoring
10-
<full_lhs_value>5\.1l</full_lhs_value>
10+
<full_lhs_value binary="[01]+">5\.1l</full_lhs_value>

regression/cbmc/float-trace/test.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <assert.h>
2+
#include <math.h>
3+
#include <stdbool.h>
4+
5+
int main()
6+
{
7+
float fzero = 0.0f;
8+
float fone = 1.0f;
9+
float fcomplex = 0x1p+37f;
10+
float finf = INFINITY;
11+
float fnan = NAN;
12+
13+
double dzero = 0.0;
14+
double done = 1.0;
15+
double dinf = INFINITY;
16+
double dnan = NAN;
17+
double dcomplex = -5.56268e-309;
18+
19+
assert(false);
20+
21+
return 0;
22+
}
23+
24+
void test(float value)
25+
{
26+
assert(value == 1.0f);
27+
assert(value == 0.0f);
28+
}

regression/cbmc/float-trace/test.desc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CORE
2+
test.c
3+
--trace --xml-ui
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
VERIFICATION FAILED
7+
<full_lhs>fzero</full_lhs>
8+
<full_lhs_value binary="0{32}">0.0f</full_lhs_value>
9+
<full_lhs>fone</full_lhs>
10+
<full_lhs_value binary="00111111100000000000000000000000">1.0f</full_lhs_value>
11+
<full_lhs>fcomplex</full_lhs>
12+
<full_lhs_value binary="01010010000000000000000000000000">1\.374390e\+11f</full_lhs_value>
13+
<full_lhs>finf</full_lhs>
14+
<full_lhs_value binary="01{8}0{23}">\+INFINITY</full_lhs_value>
15+
<full_lhs>fnan</full_lhs>
16+
<full_lhs_value binary="01{8}[01]*1[01]*">\+NAN</full_lhs_value>
17+
<full_lhs_value binary="[01]{32}">\+NAN</full_lhs_value>
18+
<full_lhs>dzero</full_lhs>
19+
<full_lhs_value binary="0{64}">0.0</full_lhs_value>
20+
<full_lhs>done</full_lhs>
21+
<full_lhs_value binary="001{10}0{52}">1.0</full_lhs_value>
22+
<full_lhs>dcomplex</full_lhs>
23+
<full_lhs_value binary="1000000000000011111111111111111111000111111100100110011101110111">-5\.562680e-309</full_lhs_value>
24+
<full_lhs>finf</full_lhs>
25+
<full_lhs_value binary="01{11}0{52}">\+INFINITY</full_lhs_value>
26+
<full_lhs>fnan</full_lhs>
27+
<full_lhs_value binary="01{11}[01]*1[01]*">\+NAN</full_lhs_value>
28+
<full_lhs_value binary="[01]{64}">\+NAN</full_lhs_value>
29+
--
30+
--
31+
Checks that the binary value is printed for floating point types
32+
point types

src/goto-programs/xml_goto_trace.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,38 @@ Author: Daniel Kroening
1919
#include <util/xml_irep.h>
2020

2121
#include <langapi/language_util.h>
22+
#include <util/arith_tools.h>
2223

2324
#include "printf_formatter.h"
2425
#include "xml_expr.h"
2526

27+
bool full_lhs_value_includes_binary(
28+
const goto_trace_stept &step,
29+
const namespacet &ns)
30+
{
31+
return can_cast_type<floatbv_typet>(step.full_lhs_value.type());
32+
}
33+
34+
xmlt full_lhs_value(const goto_trace_stept &step, const namespacet &ns)
35+
{
36+
xmlt full_lhs_value{"full_lhs_value"};
37+
38+
const auto &lhs_object = step.get_lhs_object();
39+
const irep_idt identifier =
40+
lhs_object.has_value() ? lhs_object->get_identifier() : irep_idt();
41+
42+
if(step.full_lhs_value.is_not_nil())
43+
full_lhs_value.data = from_expr(ns, identifier, step.full_lhs_value);
44+
if(full_lhs_value_includes_binary(step, ns))
45+
{
46+
const auto width = to_floatbv_type(step.full_lhs_value.type()).get_width();
47+
const auto binary_representation = integer2binary(
48+
bvrep2integer(step.full_lhs_value.get(ID_value), width, false), width);
49+
full_lhs_value.set_attribute("binary", binary_representation);
50+
}
51+
return full_lhs_value;
52+
}
53+
2654
void convert(
2755
const namespacet &ns,
2856
const goto_tracet &goto_trace,
@@ -88,16 +116,13 @@ void convert(
88116
}
89117
}
90118

91-
std::string full_lhs_string, full_lhs_value_string;
119+
std::string full_lhs_string;
92120

93121
if(step.full_lhs.is_not_nil())
94122
full_lhs_string = from_expr(ns, identifier, step.full_lhs);
95123

96-
if(step.full_lhs_value.is_not_nil())
97-
full_lhs_value_string = from_expr(ns, identifier, step.full_lhs_value);
98-
99124
xml_assignment.new_element("full_lhs").data = full_lhs_string;
100-
xml_assignment.new_element("full_lhs_value").data = full_lhs_value_string;
125+
xml_assignment.new_element(full_lhs_value(step, ns));
101126

102127
xml_assignment.set_attribute_bool("hidden", step.hidden);
103128
xml_assignment.set_attribute("thread", std::to_string(step.thread_nr));

0 commit comments

Comments
 (0)