-
Notifications
You must be signed in to change notification settings - Fork 277
Implement bswap in SAT back-end #1637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <assert.h> | ||
#include <stdint.h> | ||
|
||
#ifndef __GNUC__ | ||
uint16_t __builtin_bswap16(uint16_t); | ||
uint32_t __builtin_bswap32(uint32_t); | ||
uint64_t __builtin_bswap64(uint64_t); | ||
#endif | ||
|
||
uint16_t __VERIFIER_nondet_u16(); | ||
uint32_t __VERIFIER_nondet_u32(); | ||
uint64_t __VERIFIER_nondet_u64(); | ||
|
||
int main() | ||
{ | ||
uint16_t sa = 0xabcd; | ||
assert(__builtin_bswap16(sa) == 0xcdab); | ||
uint16_t sb = __VERIFIER_nondet_u16(); | ||
sb &= 0xff00; | ||
// expected to fail, only MSB guaranteed to be 0 | ||
assert(__builtin_bswap16(sb) == 0); | ||
assert((__builtin_bswap16(sb) & 0xff00) == 0); | ||
|
||
uint32_t a = 0xabcdef00; | ||
assert(__builtin_bswap32(a) == 0x00efcdab); | ||
uint32_t b = __VERIFIER_nondet_u32(); | ||
b &= 0xffffff00; | ||
// expected to fail, only MSB guaranteed to be 0 | ||
assert(__builtin_bswap32(b) == 0); | ||
assert((__builtin_bswap32(b) & 0xff000000) == 0); | ||
|
||
uint64_t al = 0xabcdef0001020304LL; | ||
assert(__builtin_bswap64(al) == 0x0403020100efcdabLL); | ||
uint64_t bl = __VERIFIER_nondet_u64(); | ||
bl &= 0xffffffffffffff00LL; | ||
// expected to fail, only MSB guaranteed to be 0 | ||
assert(__builtin_bswap64(bl) == 0); | ||
assert((__builtin_bswap64(bl) & 0xff00000000000000) == 0); | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CORE | ||
main.c | ||
|
||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
\[main\.assertion\.[258]\] assertion __builtin_bswap(16|32|64)\((sb|b|bl)\) == 0: FAILURE$ | ||
^\*\* 3 of 9 failed | ||
-- | ||
^warning: ignoring | ||
\[main\.assertion\.[258]\] assertion __builtin_bswap(16|32|64)\((sb|b|bl)\) == 0: SUCCESS$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef _WIN32 | ||
#include <arpa/inet.h> | ||
#include <assert.h> | ||
|
||
int main() | ||
{ | ||
uint32_t l; | ||
assert(l == ntohl(htonl(l))); | ||
|
||
uint16_t s; | ||
assert(s == ntohs(htons(s))); | ||
|
||
return 0; | ||
} | ||
|
||
#else | ||
|
||
int main() | ||
{ | ||
return 0; | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
main.c | ||
|
||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
^warning: ignoring |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Bit-blasting of bswap | ||
|
||
Author: Michael Tautschnig | ||
|
||
\*******************************************************************/ | ||
|
||
#include "boolbv.h" | ||
|
||
#include <util/config.h> | ||
#include <util/invariant.h> | ||
|
||
bvt boolbvt::convert_bswap(const bswap_exprt &expr) | ||
{ | ||
const std::size_t width = boolbv_width(expr.type()); | ||
|
||
// width must be multiple of bytes | ||
const std::size_t byte_bits = config.ansi_c.char_width; | ||
if(width % byte_bits != 0) | ||
return conversion_failed(expr); | ||
|
||
bvt result = convert_bv(expr.op()); | ||
CHECK_RETURN(result.size() == width); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tidy! I also note that convert_bv is correct, convert_bitvector is not. |
||
|
||
std::size_t dest_base = width; | ||
|
||
for(std::size_t src = 0; src < width; ++src) | ||
{ | ||
std::size_t bit_offset = src % byte_bits; | ||
if(bit_offset == 0) | ||
dest_base -= byte_bits; | ||
|
||
if(src >= dest_base) | ||
break; | ||
|
||
result[src].swap(result[dest_base + bit_offset]); | ||
} | ||
|
||
return result; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ Author: Daniel Kroening, [email protected] | |
#include "mp_arith.h" | ||
#include "replace_expr.h" | ||
|
||
class bswap_exprt; | ||
class byte_extract_exprt; | ||
class byte_update_exprt; | ||
class exprt; | ||
|
@@ -100,7 +101,7 @@ class simplify_exprt | |
bool simplify_dereference(exprt &expr); | ||
bool simplify_address_of(exprt &expr); | ||
bool simplify_pointer_offset(exprt &expr); | ||
bool simplify_bswap(exprt &expr); | ||
bool simplify_bswap(bswap_exprt &expr); | ||
bool simplify_isinf(exprt &expr); | ||
bool simplify_isnan(exprt &expr); | ||
bool simplify_isnormal(exprt &expr); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,16 +23,13 @@ Author: Daniel Kroening, [email protected] | |
#include "rational_tools.h" | ||
#include "ieee_float.h" | ||
|
||
bool simplify_exprt::simplify_bswap(exprt &expr) | ||
bool simplify_exprt::simplify_bswap(bswap_exprt &expr) | ||
{ | ||
if(expr.type().id()==ID_unsignedbv && | ||
expr.operands().size()==1 && | ||
expr.op0().type()==expr.type() && | ||
expr.op0().is_constant()) | ||
if(expr.type().id() == ID_unsignedbv && expr.op().is_constant()) | ||
{ | ||
std::size_t width=to_bitvector_type(expr.type()).get_width(); | ||
mp_integer value; | ||
to_integer(expr.op0(), value); | ||
to_integer(expr.op(), value); | ||
std::vector<mp_integer> bytes; | ||
|
||
// take apart | ||
|
@@ -48,7 +45,8 @@ bool simplify_exprt::simplify_bswap(exprt &expr) | |
bytes.pop_back(); | ||
} | ||
|
||
expr=from_integer(new_value, expr.type()); | ||
constant_exprt c = from_integer(new_value, expr.type()); | ||
expr.swap(c); | ||
return false; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be unknown instead of failing if it's unknown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that asking to clarify the comment (which should likely say "may fail")?