Skip to content

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

Merged
merged 5 commits into from
Mar 11, 2019

Conversation

antlechner
Copy link
Contributor

This PR includes no functional changes, only refactoring.

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • n/a Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • n/a My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

We should only store these arithmetic calculations in one place in the
codebase.
Copy link
Collaborator

@martin-cs martin-cs left a 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!

mp_integer largest() const;
constant_exprt smallest_expr() const;
constant_exprt zero_expr() const;
constant_exprt largest_expr() const;
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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())
Copy link
Collaborator

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()?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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).

}
else
UNREACHABLE;
const mp_integer max = bv_spect(envp_size_symbol.type).max_value();
Copy link
Collaborator

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()?

Copy link
Contributor

@allredj allredj left a 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

@antlechner antlechner force-pushed the antonia/bv-min-max-refactor branch from 23c17f1 to c8f8458 Compare March 8, 2019 16:56
Copy link
Contributor

@allredj allredj left a 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.
@antlechner antlechner force-pushed the antonia/bv-min-max-refactor branch from c8f8458 to f313fdf Compare March 8, 2019 19:14
Copy link
Contributor

@allredj allredj left a 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

@tautschnig tautschnig merged commit 5973f10 into diffblue:develop Mar 11, 2019
@antlechner antlechner deleted the antonia/bv-min-max-refactor branch March 11, 2019 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants