-
Notifications
You must be signed in to change notification settings - Fork 273
smt2: bswap and popcount #2246
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
smt2: bswap and popcount #2246
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
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 |
---|---|---|
|
@@ -29,9 +29,10 @@ Author: Daniel Kroening, [email protected] | |
#include <util/string_constant.h> | ||
|
||
#include <solvers/flattening/boolbv_width.h> | ||
#include <solvers/flattening/flatten_byte_operators.h> | ||
#include <solvers/flattening/c_bit_field_replacement_type.h> | ||
#include <solvers/flattening/flatten_byte_operators.h> | ||
#include <solvers/floatbv/float_bv.h> | ||
#include <solvers/lowering/expr_lowering.h> | ||
|
||
// Mark different kinds of error condition | ||
// General | ||
|
@@ -1809,6 +1810,68 @@ void smt2_convt::convert_expr(const exprt &expr) | |
"smt2_convt::convert_expr: `"+expr.id_string()+ | ||
"' is not yet supported"); | ||
} | ||
else if(expr.id() == ID_bswap) | ||
{ | ||
if(expr.operands().size() != 1) | ||
INVALIDEXPR("bswap gets one operand"); | ||
|
||
if(expr.op0().type() != expr.type()) | ||
INVALIDEXPR("bswap gets one operand with same type"); | ||
|
||
// first 'let' the operand | ||
out << "(let ((bswap_op "; | ||
convert_expr(expr.op0()); | ||
out << ")) "; | ||
|
||
if(expr.type().id() == ID_signedbv || expr.type().id() == ID_unsignedbv) | ||
{ | ||
const std::size_t width = to_bitvector_type(expr.type()).get_width(); | ||
|
||
// width must be multiple of bytes | ||
if(width % 8 != 0) | ||
INVALIDEXPR("bswap must get bytes"); | ||
|
||
const std::size_t bytes = width / 8; | ||
|
||
if(bytes <= 1) | ||
out << "bswap_op"; | ||
else | ||
{ | ||
// do a parallel 'let' for each byte | ||
out << "(let ("; | ||
|
||
for(std::size_t byte = 0; byte < bytes; byte++) | ||
{ | ||
if(byte != 0) | ||
out << ' '; | ||
out << "(bswap_byte_" << byte << ' '; | ||
out << "((_ extract " << (byte * 8 + 7) << " " << (byte * 8) | ||
<< ") bswap_op)"; | ||
out << ')'; | ||
} | ||
|
||
out << ") "; | ||
|
||
// now stitch back together with 'concat' | ||
out << "(concat"; | ||
|
||
for(std::size_t byte = 0; byte < bytes; byte++) | ||
out << " bswap_byte_" << byte; | ||
|
||
out << ')'; // concat | ||
out << ')'; // let bswap_byte_* | ||
} | ||
} | ||
else | ||
UNEXPECTEDCASE("bswap must get bitvector operand"); | ||
|
||
out << ')'; // let bswap_op | ||
} | ||
else if(expr.id() == ID_popcount) | ||
{ | ||
exprt lowered = lower_popcount(to_popcount_expr(expr), ns); | ||
convert_expr(lowered); | ||
} | ||
else | ||
UNEXPECTEDCASE( | ||
"smt2_convt::convert_expr: `"+expr.id_string()+"' is unsupported"); | ||
|
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
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.
Could
config.ansi_c.char_width
please be used so that #917 doesn't end up in an endless catch-up game?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.
I did contemplate this; would the semantics of __builtin_bswapX actually change on a machine with chars that are >8 bits?
The gcc manual says "Byte here always means exactly 8 bits."
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.
I'd rather insist that
exprt(ID_bswap)
has a semantics that may differ from that of__builtin_bswapX
; if there are cases where__builtin_bswapX
does not swap bytes (of some number of bits) then it's up to the front-end to sort this out. If we wantexprt(ID_bswap)
to always swap multiples of 8 bits then we should 1) rename it and 2) changeboolbvt::convert_bswap
to follow this semantics.The one case (TI C55x) of a non-8-bit-bytes architecture that I know of doesn't have GCC support so
__builtin_bswapX
wouldn't actually occur.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.
Ok, then I'd advocate to make "number of bits in a byte" a parameter, so we don't rely on a global config.
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.
That could be done, but then we've still got
byte_{extract,update}
all the way to the back-end, which also need to know about the width of a byte. I'd just like to come up with a consistent solution for all of those.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.
I'd probably go for the same here.