Skip to content

Commit 9a67ec4

Browse files
committed
Lowering of byte_extract over string constants
These need to be handled like arrays of characters.
1 parent c580d94 commit 9a67ec4

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

@@ -275,6 +276,26 @@ static exprt unpack_rec(
275276
ns,
276277
unpack_byte_array);
277278
}
279+
else if(src.id() == ID_string_constant)
280+
{
281+
return unpack_rec(
282+
to_string_constant(src).to_array_expr(),
283+
little_endian,
284+
offset_bytes,
285+
max_bytes,
286+
ns,
287+
unpack_byte_array);
288+
}
289+
else if(src.id() == ID_constant && src.type().id() == ID_string)
290+
{
291+
return unpack_rec(
292+
string_constantt(to_constant_expr(src).get_value()).to_array_expr(),
293+
little_endian,
294+
offset_bytes,
295+
max_bytes,
296+
ns,
297+
unpack_byte_array);
298+
}
278299
else if(src.type().id()!=ID_empty)
279300
{
280301
// 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)