-
Notifications
You must be signed in to change notification settings - Fork 274
Refactor code related to bitvector type min/max values #4352
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
Refactor code related to bitvector type min/max values #4352
Conversation
We should only store these arithmetic calculations in one place in the codebase.
68ef509
to
23c17f1
Compare
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.
Seems reasonable and code reduction patches are always a good thing!
src/util/std_types.h
Outdated
mp_integer largest() const; | ||
constant_exprt smallest_expr() const; | ||
constant_exprt zero_expr() const; | ||
constant_exprt largest_expr() const; |
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.
This is brilliant refactoring, thank you! Yet can you please hide these in pointer_typet
? A pointer_typet
happens to be a bitvector_typet
, but really they must not be used (not even zero_expr
) on a pointer_typet
.
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.
Ah, I overlooked that there are actually quite a few classes that inherit from bitvector_typet
. Besides the three that were already mentioned, there are bv_typet
, fixedbv_typet
, floatbv_typet
, c_bit_field_typet
and c_bool_typet
. However these functions will hit an UNREACHABLE
if the type id is anything other than ID_unsignedbv
or ID_signedbv
. Would it make sense to introduce a new class that inherits from bitvector_typet
and that both signedbv_typet
and unsignedbv_typet
inherit from? Any suggestions for what such a class should be called?
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.
smallest()/largest() only make sense for the integer ones -- so that could be integer_bitvector_typet. smallest_expr, zero_expr, largest_expr() also work on fixedbv and floatbv; these could be a numeric_bitvector_typet.
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 makes sense, thank you! I only created the integer_bitvector_typet
class as all five methods were previously only defined for the signedbv/unsignedbv types, and I added some documentation and a comment mentioning that the methods returning an exprt would make sense for fixedbv/floatbv as well in case we need them there in the future.
@@ -458,7 +446,7 @@ void initialize_nondet_string_fields( | |||
code.add(code_assumet(binary_relation_exprt(length_expr, ID_ge, min_length))); | |||
|
|||
// assume (length_expr <= max_input_length) | |||
if(max_nondet_string_length <= max_value(length_expr.type())) | |||
if(max_nondet_string_length <= bv_spect(length_expr.type()).max_value()) |
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.
How about to_bitvector_type(length_expr.type()).largest()
?
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.
Good point, why does the class bv_spect
even (publicly) exist if this functionality is already made available by the typet?
I changed it to your suggestion for now. Depending on what you think about my previous question I might change to_bitvector_type
to something more specific.
I also found another place where bv_spect
was used to get the max value, and replaced this with bitvector_type::largest
as well.
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.
Following @kroening 's comment above, this is now to_integer_bitvector_type(...)
(and similarly for your other comment).
src/ansi-c/ansi_c_entry_point.cpp
Outdated
} | ||
else | ||
UNREACHABLE; | ||
const mp_integer max = bv_spect(envp_size_symbol.type).max_value(); |
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.
As above to_bitvector_type(envp_size_symbol.type).largest()
?
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: 23c17f1).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/103691044
23c17f1
to
c8f8458
Compare
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: c8f8458).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/103716955
Since unsignedbv_typet and signedbv_typet now have some common functionality, it makes sense to give them a common parent to store these methods in.
These methods are now all identical and can be inherited from integer_bitvector_typet.
The method can be used in several places that previously duplicated its logic.
c8f8458
to
f313fdf
Compare
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.
✔️
Passed Diffblue compatibility checks (cbmc commit: f313fdf).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/103736558
This PR includes no functional changes, only refactoring.