Skip to content

Commit 4d4e564

Browse files
committed
Lowering of byte_extract over string constants
These need to be handled like arrays of characters.
1 parent 7632d02 commit 4d4e564

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/solvers/lowering/byte_operators.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/pointer_offset_size.h>
1616
#include <util/replace_symbol.h>
1717
#include <util/simplify_expr.h>
18+
#include <util/string_constant.h>
1819

1920
#include "flatten_byte_extract_exceptions.h"
2021

@@ -264,6 +265,26 @@ static exprt unpack_rec(
264265
member, little_endian, offset_bytes, max_bytes, ns, true);
265266
}
266267
}
268+
else if(src.id() == ID_string_constant)
269+
{
270+
return unpack_rec(
271+
to_string_constant(src).to_array_expr(),
272+
little_endian,
273+
offset_bytes,
274+
max_bytes,
275+
ns,
276+
unpack_byte_array);
277+
}
278+
else if(src.id() == ID_constant && src.type().id() == ID_string)
279+
{
280+
return unpack_rec(
281+
string_constantt(to_constant_expr(src).get_value()).to_array_expr(),
282+
little_endian,
283+
offset_bytes,
284+
max_bytes,
285+
ns,
286+
unpack_byte_array);
287+
}
267288
else if(src.type().id()!=ID_empty)
268289
{
269290
// a basic type; we turn that into extractbits while considering

unit/solvers/lowering/byte_operators.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <util/simplify_expr.h>
2222
#include <util/simplify_expr_class.h>
2323
#include <util/std_types.h>
24+
#include <util/string_constant.h>
2425
#include <util/symbol_table.h>
2526

2627
SCENARIO("byte_extract_lowering", "[core][solvers][lowering][byte_extract]")
@@ -90,6 +91,35 @@ SCENARIO("byte_extract_lowering", "[core][solvers][lowering][byte_extract]")
9091
}
9192
}
9293

94+
GIVEN("A a byte_extract from a string constant")
95+
{
96+
string_constantt s("ABCD");
97+
const byte_extract_exprt be1(
98+
ID_byte_extract_little_endian,
99+
s,
100+
from_integer(1, index_type()),
101+
unsignedbv_typet(16));
102+
103+
THEN("byte_extract lowering yields the expected value")
104+
{
105+
const exprt lower_be1 = lower_byte_extract(be1, ns);
106+
107+
REQUIRE(!has_subexpr(lower_be1, ID_byte_extract_little_endian));
108+
REQUIRE(lower_be1.type() == be1.type());
109+
REQUIRE(
110+
lower_be1 == from_integer((int('C') << 8) + 'B', unsignedbv_typet(16)));
111+
112+
byte_extract_exprt be2 = be1;
113+
be2.id(ID_byte_extract_big_endian);
114+
const exprt lower_be2 = lower_byte_extract(be2, ns);
115+
116+
REQUIRE(!has_subexpr(lower_be2, ID_byte_extract_big_endian));
117+
REQUIRE(lower_be2.type() == be2.type());
118+
REQUIRE(
119+
lower_be2 == from_integer((int('B') << 8) + 'C', unsignedbv_typet(16)));
120+
}
121+
}
122+
93123
GIVEN("A collection of types")
94124
{
95125
unsignedbv_typet u8(8);

0 commit comments

Comments
 (0)