Skip to content

Commit 6c3918a

Browse files
author
Daniel Kroening
authored
Merge pull request #3155 from tautschnig/fix-simplify-byte-extract
Fix typo in simplify_byte_extract
2 parents 97c6c55 + 42c6746 commit 6c3918a

File tree

4 files changed

+88
-49
lines changed

4 files changed

+88
-49
lines changed

jbmc/unit/util/simplify_expr.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,12 @@
99
#include <testing-utils/catch.hpp>
1010

1111
#include <java_bytecode/java_types.h>
12-
#include <util/arith_tools.h>
13-
#include <util/c_types.h>
1412
#include <util/config.h>
1513
#include <util/namespace.h>
16-
#include <util/pointer_predicates.h>
1714
#include <util/simplify_expr.h>
1815
#include <util/std_expr.h>
1916
#include <util/symbol_table.h>
2017

21-
TEST_CASE("Simplify pointer_offset(address of array index)")
22-
{
23-
config.set_arch("none");
24-
25-
symbol_tablet symbol_table;
26-
namespacet ns(symbol_table);
27-
28-
array_typet array_type(char_type(), from_integer(2, size_type()));
29-
symbol_exprt array("A", array_type);
30-
index_exprt index(array, from_integer(1, index_type()));
31-
address_of_exprt address_of(index);
32-
33-
exprt p_o=pointer_offset(address_of);
34-
35-
exprt simp=simplify_expr(p_o, ns);
36-
37-
REQUIRE(simp.id()==ID_constant);
38-
mp_integer offset_value;
39-
REQUIRE(!to_integer(simp, offset_value));
40-
REQUIRE(offset_value==1);
41-
}
42-
43-
TEST_CASE("Simplify const pointer offset")
44-
{
45-
config.set_arch("none");
46-
47-
symbol_tablet symbol_table;
48-
namespacet ns(symbol_table);
49-
50-
// build a numeric constant of some pointer type
51-
constant_exprt number=from_integer(1234, size_type());
52-
number.type()=pointer_type(char_type());
53-
54-
exprt p_o=pointer_offset(number);
55-
56-
exprt simp=simplify_expr(p_o, ns);
57-
58-
REQUIRE(simp.id()==ID_constant);
59-
mp_integer offset_value;
60-
REQUIRE(!to_integer(simp, offset_value));
61-
REQUIRE(offset_value==1234);
62-
}
63-
6418
namespace
6519
{
6620

src/util/simplify_expr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,9 +1722,9 @@ bool simplify_exprt::simplify_byte_extract(byte_extract_exprt &expr)
17221722
// byte extract of full object is object
17231723
// don't do any of the following if endianness doesn't match, as
17241724
// bytes need to be swapped
1725-
if(offset==0 &&
1726-
base_type_eq(expr.type(), expr.op().type(), ns) &&
1727-
byte_extract_id()!=expr.id())
1725+
if(
1726+
offset == 0 && base_type_eq(expr.type(), expr.op().type(), ns) &&
1727+
byte_extract_id() == expr.id())
17281728
{
17291729
exprt tmp=expr.op();
17301730
expr.swap(tmp);

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SRC += analyses/ai/ai.cpp \
4343
util/replace_symbol.cpp \
4444
util/sharing_map.cpp \
4545
util/sharing_node.cpp \
46+
util/simplify_expr.cpp \
4647
util/small_map.cpp \
4748
util/small_shared_two_way_ptr.cpp \
4849
util/std_expr.cpp \

unit/util/simplify_expr.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests of the expression simplifier
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/catch.hpp>
10+
11+
#include <util/arith_tools.h>
12+
#include <util/byte_operators.h>
13+
#include <util/c_types.h>
14+
#include <util/cmdline.h>
15+
#include <util/config.h>
16+
#include <util/namespace.h>
17+
#include <util/pointer_predicates.h>
18+
#include <util/simplify_expr.h>
19+
#include <util/std_expr.h>
20+
#include <util/symbol_table.h>
21+
22+
TEST_CASE("Simplify pointer_offset(address of array index)")
23+
{
24+
config.set_arch("none");
25+
26+
symbol_tablet symbol_table;
27+
namespacet ns(symbol_table);
28+
29+
array_typet array_type(char_type(), from_integer(2, size_type()));
30+
symbol_exprt array("A", array_type);
31+
index_exprt index(array, from_integer(1, index_type()));
32+
address_of_exprt address_of(index);
33+
34+
exprt p_o=pointer_offset(address_of);
35+
36+
exprt simp=simplify_expr(p_o, ns);
37+
38+
REQUIRE(simp.id()==ID_constant);
39+
mp_integer offset_value;
40+
REQUIRE(!to_integer(simp, offset_value));
41+
REQUIRE(offset_value==1);
42+
}
43+
44+
TEST_CASE("Simplify const pointer offset")
45+
{
46+
config.set_arch("none");
47+
48+
symbol_tablet symbol_table;
49+
namespacet ns(symbol_table);
50+
51+
// build a numeric constant of some pointer type
52+
constant_exprt number=from_integer(1234, size_type());
53+
number.type()=pointer_type(char_type());
54+
55+
exprt p_o=pointer_offset(number);
56+
57+
exprt simp=simplify_expr(p_o, ns);
58+
59+
REQUIRE(simp.id()==ID_constant);
60+
mp_integer offset_value;
61+
REQUIRE(!to_integer(simp, offset_value));
62+
REQUIRE(offset_value==1234);
63+
}
64+
65+
TEST_CASE("Simplify byte extract")
66+
{
67+
// this test does require a proper architecture to be set so that byte extract
68+
// uses adequate endianness
69+
cmdlinet cmdline;
70+
config.set(cmdline);
71+
72+
symbol_tablet symbol_table;
73+
namespacet ns(symbol_table);
74+
75+
// byte-extracting type T at offset 0 from an object of type T yields the
76+
// object
77+
symbol_exprt s("foo", size_type());
78+
byte_extract_exprt be(
79+
byte_extract_id(), s, from_integer(0, index_type()), size_type());
80+
81+
exprt simp = simplify_expr(be, ns);
82+
83+
REQUIRE(simp == s);
84+
}

0 commit comments

Comments
 (0)