Refactored sym-exec: Added comments, changed members' names ...

This commit is contained in:
Mariam Arutunian 2024-04-15 15:36:18 +04:00
parent 4953a04ee6
commit 525b7590e3
6 changed files with 269 additions and 129 deletions

View File

@ -2,24 +2,24 @@
bit_condition::bit_condition (value_bit *left, value_bit *right, tree_code code) bit_condition::bit_condition (value_bit *left, value_bit *right, tree_code code)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
this->code = code; this->m_code = code;
type = BIT_CONDITION; m_type = BIT_CONDITION;
} }
bit_condition::bit_condition (const bit_condition &expr) bit_condition::bit_condition (const bit_condition &expr)
{ {
bit_expression::copy (&expr); bit_expression::copy (&expr);
code = expr.get_code (); m_code = expr.get_code ();
} }
tree_code tree_code
bit_condition::get_code () const bit_condition::get_code () const
{ {
return code; return m_code;
} }
@ -33,7 +33,7 @@ bit_condition::copy () const
void void
bit_condition::print_expr_sign () bit_condition::print_expr_sign ()
{ {
switch (code) switch (m_code)
{ {
case GT_EXPR: case GT_EXPR:
fprintf (dump_file, " > "); fprintf (dump_file, " > ");

View File

@ -13,7 +13,7 @@ enum condition_status {
class bit_condition : public bit_expression { class bit_condition : public bit_expression {
private: private:
tree_code code; tree_code m_code;
void print_expr_sign (); void print_expr_sign ();
public: public:

View File

@ -7,76 +7,76 @@
value_type value_type
value_bit::get_type () const value_bit::get_type () const
{ {
return type; return m_type;
}
symbolic_bit::symbolic_bit (size_t i, tree orig)
: value_bit (i), m_origin (orig)
{
m_type = SYMBOLIC_BIT;
} }
symbolic_bit::symbolic_bit (size_t i, tree orig) : value_bit (i), origin (orig) bit::bit (unsigned char i) : m_val (i)
{ {
type = SYMBOLIC_BIT; m_type = BIT;
}
bit::bit (unsigned char i) : val (i)
{
type = BIT;
} }
value_bit * value_bit *
bit_expression::get_left () bit_expression::get_left ()
{ {
return left; return m_left;
} }
value_bit * value_bit *
bit_expression::get_right () bit_expression::get_right ()
{ {
return right; return m_right;
} }
void void
bit_expression::set_left (value_bit *expr) bit_expression::set_left (value_bit *expr)
{ {
left = expr; m_left = expr;
} }
void void
bit_expression::set_right (value_bit *expr) bit_expression::set_right (value_bit *expr)
{ {
right = expr; m_right = expr;
} }
size_t size_t
value_bit::get_index () const value_bit::get_index () const
{ {
return index; return m_index;
} }
unsigned char unsigned char
bit::get_val () const bit::get_val () const
{ {
return val; return m_val;
} }
void void
bit::set_val (unsigned char new_val) bit::set_val (unsigned char new_val)
{ {
val = new_val; m_val = new_val;
} }
bit_complement_expression::bit_complement_expression (value_bit *right) bit_complement_expression::bit_complement_expression (value_bit *right)
{ {
this->left = nullptr; this->m_left = nullptr;
this->right = right; this->m_right = right;
type = BIT_COMPLEMENT_EXPRESSION; m_type = BIT_COMPLEMENT_EXPRESSION;
} }
@ -89,10 +89,10 @@ bit_complement_expression::bit_complement_expression (
bit_expression::~bit_expression () bit_expression::~bit_expression ()
{ {
delete left; delete m_left;
left = nullptr; m_left = nullptr;
delete right; delete m_right;
right = nullptr; m_right = nullptr;
} }
@ -113,13 +113,13 @@ bit::copy () const
void void
bit_expression::copy (const bit_expression *expr) bit_expression::copy (const bit_expression *expr)
{ {
if (expr->left) if (expr->m_left)
left = expr->left->copy (); m_left = expr->m_left->copy ();
if (expr->right) if (expr->m_right)
right = expr->right->copy (); m_right = expr->m_right->copy ();
type = expr->type; m_type = expr->m_type;
} }
@ -181,9 +181,9 @@ bit_complement_expression::copy () const
bit_xor_expression::bit_xor_expression (value_bit *left, value_bit *right) bit_xor_expression::bit_xor_expression (value_bit *left, value_bit *right)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
type = BIT_XOR_EXPRESSION; m_type = BIT_XOR_EXPRESSION;
} }
@ -195,9 +195,9 @@ bit_xor_expression::bit_xor_expression (const bit_xor_expression &expr)
bit_and_expression::bit_and_expression (value_bit *left, value_bit *right) bit_and_expression::bit_and_expression (value_bit *left, value_bit *right)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
type = BIT_AND_EXPRESSION; m_type = BIT_AND_EXPRESSION;
} }
@ -209,9 +209,9 @@ bit_and_expression::bit_and_expression (const bit_and_expression &expr)
bit_or_expression::bit_or_expression (value_bit *left, value_bit *right) bit_or_expression::bit_or_expression (value_bit *left, value_bit *right)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
type = BIT_OR_EXPRESSION; m_type = BIT_OR_EXPRESSION;
} }
@ -224,9 +224,9 @@ bit_or_expression::bit_or_expression (const bit_or_expression &expr)
shift_right_expression::shift_right_expression (value_bit *left, shift_right_expression::shift_right_expression (value_bit *left,
value_bit *right) value_bit *right)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
type = SHIFT_RIGHT_EXPRESSION; m_type = SHIFT_RIGHT_EXPRESSION;
} }
@ -239,9 +239,9 @@ shift_right_expression::shift_right_expression (
shift_left_expression::shift_left_expression (value_bit *left, value_bit *right) shift_left_expression::shift_left_expression (value_bit *left, value_bit *right)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
type = SHIFT_LEFT_EXPRESSION; m_type = SHIFT_LEFT_EXPRESSION;
} }
@ -253,9 +253,9 @@ shift_left_expression::shift_left_expression (const shift_left_expression &expr)
add_expression::add_expression (value_bit *left, value_bit *right) add_expression::add_expression (value_bit *left, value_bit *right)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
type = ADD_EXPRESSION; m_type = ADD_EXPRESSION;
} }
@ -267,9 +267,9 @@ add_expression::add_expression (const add_expression &expr)
sub_expression::sub_expression (value_bit *left, value_bit *right) sub_expression::sub_expression (value_bit *left, value_bit *right)
{ {
this->left = left; this->m_left = left;
this->right = right; this->m_right = right;
type = SUB_EXPRESSION; m_type = SUB_EXPRESSION;
} }
@ -282,7 +282,7 @@ sub_expression::sub_expression (const sub_expression &expr)
tree tree
symbolic_bit::get_origin () symbolic_bit::get_origin ()
{ {
return origin; return m_origin;
} }
@ -291,8 +291,8 @@ symbolic_bit::print ()
{ {
if (dump_file) if (dump_file)
{ {
print_generic_expr (dump_file, origin, dump_flags); print_generic_expr (dump_file, m_origin, dump_flags);
fprintf (dump_file, "[%lu]", index); fprintf (dump_file, "[%zu]", m_index);
} }
} }
@ -301,14 +301,14 @@ void
bit::print () bit::print ()
{ {
if (dump_file) if (dump_file)
fprintf (dump_file, "%u", val); fprintf (dump_file, "%u", m_val);
} }
void void
bit_expression::print_expr_sign () bit_expression::print_expr_sign ()
{ {
switch (type) switch (m_type)
{ {
case BIT_XOR_EXPRESSION: case BIT_XOR_EXPRESSION:
fprintf (dump_file, " ^ "); fprintf (dump_file, " ^ ");
@ -343,15 +343,15 @@ bit_expression::print ()
if (dump_file) if (dump_file)
{ {
fprintf (dump_file, "("); fprintf (dump_file, "(");
if (left) if (m_left)
left->print (); m_left->print ();
else else
fprintf (dump_file, "null"); fprintf (dump_file, "null");
print_expr_sign (); print_expr_sign ();
if (right) if (m_right)
right->print (); m_right->print ();
else else
fprintf (dump_file, "null"); fprintf (dump_file, "null");
@ -366,8 +366,8 @@ bit_complement_expression::print ()
if (dump_file) if (dump_file)
{ {
fprintf (dump_file, "!"); fprintf (dump_file, "!");
if (right) if (m_right)
right->print (); m_right->print ();
else else
fprintf (dump_file, "null"); fprintf (dump_file, "null");
} }

View File

@ -38,15 +38,15 @@ class value_bit {
protected: protected:
/* This will help us to understand where is moved the bit /* This will help us to understand where is moved the bit
from its initial position. */ from its initial position. */
const size_t index; const size_t m_index;
value_type type; value_type m_type;
public: public:
value_bit () : index (0) value_bit () : m_index (0)
{}; {};
value_bit (size_t i) : index (i) value_bit (size_t i) : m_index (i)
{}; {};
value_bit (const value_bit &val) : index (val.index) value_bit (const value_bit &val) : m_index (val.m_index)
{}; {};
size_t get_index () const; size_t get_index () const;
value_type get_type () const; value_type get_type () const;
@ -60,12 +60,12 @@ class value_bit {
/* Represents value of a single bit of symbolic marked variables. */ /* Represents value of a single bit of symbolic marked variables. */
class symbolic_bit : public value_bit { class symbolic_bit : public value_bit {
tree origin = nullptr; tree m_origin = nullptr;
public: public:
symbolic_bit (size_t i, tree orig); symbolic_bit (size_t i, tree orig);
symbolic_bit (const symbolic_bit &sym_bit) : symbolic_bit (sym_bit.index, symbolic_bit (const symbolic_bit &sym_bit) : symbolic_bit (sym_bit.m_index,
sym_bit.origin) sym_bit.m_origin)
{}; {};
value_bit *copy () const; value_bit *copy () const;
@ -79,11 +79,11 @@ class symbolic_bit : public value_bit {
class bit : public value_bit { class bit : public value_bit {
private: private:
/* This is the value of a bit. It must be either 1 or 0. */ /* This is the value of a bit. It must be either 1 or 0. */
unsigned char val = 0; unsigned char m_val = 0;
public: public:
bit (unsigned char i); bit (unsigned char i);
bit (const bit &b) : bit (b.val) bit (const bit &b) : bit (b.m_val)
{}; {};
unsigned char get_val () const; unsigned char get_val () const;
void set_val (unsigned char new_val); void set_val (unsigned char new_val);
@ -93,12 +93,12 @@ class bit : public value_bit {
/* Bit-level base expression class. In general expressions consist of /* Bit-level base expression class. In general expressions consist of
two operands. Here we named them left and right. */ two operands. Here we named them m_left and right. */
class bit_expression : public value_bit { class bit_expression : public value_bit {
protected: protected:
value_bit *left = nullptr; value_bit *m_left = nullptr;
value_bit *right = nullptr; value_bit *m_right = nullptr;
void copy (const bit_expression *expr); void copy (const bit_expression *expr);
virtual void print_expr_sign (); virtual void print_expr_sign ();

View File

@ -43,20 +43,35 @@ state::is_declared (tree var)
} }
/* Declares given variable if it has not been declared yet. */
void void
state::declare_if_needed (tree var, size_t size) state::declare_if_needed (tree var, size_t size)
{ {
if (TREE_CODE (var) != INTEGER_CST && !is_declared (var)) if (TREE_CODE (var) != INTEGER_CST && !is_declared (var))
make_symbolic (var, size); {
make_symbolic (var, size);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file,
"Declaring var ");
print_generic_expr (dump_file, var, dump_flags);
fprintf (dump_file,
" with size %zd\n", size);
}
}
} }
/* Get value of the given variable. */
value * value *
state::get_value (tree var) state::get_value (tree var)
{ {
return var_states.get (var); return var_states.get (var);
} }
/* Get the value of the tree, which is in the beginning of the var_states. */ /* Get the value of the tree, which is in the beginning of the var_states. */
value * value *
@ -65,6 +80,9 @@ state::get_first_value ()
return &(*(var_states.begin ())).second; return &(*(var_states.begin ())).second;
} }
/* Returns the list of conditions in the state. */
const hash_set<bit_expression *> & const hash_set<bit_expression *> &
state::get_conditions () state::get_conditions ()
{ {
@ -112,6 +130,8 @@ state::create_val_for_const (tree var, size_t size)
} }
/* Adds the given variable to state. */
bool bool
state::add_var_state (tree var, value *vstate) state::add_var_state (tree var, value *vstate)
{ {
@ -124,6 +144,8 @@ state::add_var_state (tree var, value *vstate)
} }
/* Adds the given condition to the state. */
bool bool
state::add_condition (bit_expression *cond) state::add_condition (bit_expression *cond)
{ {
@ -131,6 +153,8 @@ state::add_condition (bit_expression *cond)
} }
/* Bulk add the given conditions to the state. */
bool bool
state::bulk_add_conditions (const hash_set<bit_expression *> &conds) state::bulk_add_conditions (const hash_set<bit_expression *> &conds)
{ {
@ -142,14 +166,25 @@ state::bulk_add_conditions (const hash_set<bit_expression *> &conds)
} }
/* Remove all states from the states' vector. */
void void
state::clear_states (vec<state *> *states) state::remove_states (vec<state *> *states)
{ {
while (!states->is_empty ()) while (!states->is_empty ())
{ {
delete states->last (); delete states->last ();
states->pop (); states->pop ();
} }
}
/* Remove all states from the states' vector and release the vector. */
void
state::clear_states (vec<state *> *states)
{
remove_states (states);
states->release (); states->release ();
} }
@ -283,7 +318,7 @@ state::make_symbolic (tree var, unsigned size)
} }
/* Performs AND operation on two values. */ /* Performs AND operation on two bits. */
value_bit * value_bit *
state::and_two_bits (value_bit *arg1, value_bit *arg2) state::and_two_bits (value_bit *arg1, value_bit *arg2)
@ -308,14 +343,20 @@ state::and_two_bits (value_bit *arg1, value_bit *arg2)
} }
/* A wrapper for operations on two bits.
Operation and operands are passed as arguments. */
value_bit * value_bit *
state::operate_bits (bit_func bit_op, value_bit *bit1, value_bit *bit2, state::operate_bits (bit_func bit_op, value_bit *bit1, value_bit *bit2,
value_bit **bit3) value_bit **)
{ {
return (bit_op) (bit1, bit2); return (bit_op) (bit1, bit2);
} }
/* A wrapper for operations on three bits.
Operation and operands are passed as arguments. */
value_bit * value_bit *
state::operate_bits (bit_func3 bit_op, value_bit *bit1, value_bit *bit2, state::operate_bits (bit_func3 bit_op, value_bit *bit1, value_bit *bit2,
value_bit **bit3) value_bit **bit3)
@ -371,6 +412,8 @@ state::do_and (tree arg1, tree arg2, tree dest)
} }
/* Performs AND operation on given values. The result is stored in dest. */
void void
state::do_and (value *arg1, value *arg2, tree dest) state::do_and (value *arg1, value *arg2, tree dest)
{ {
@ -416,6 +459,8 @@ state::do_or (tree arg1, tree arg2, tree dest)
} }
/* Performs OR operation on given values. The result is stored in dest. */
void void
state::do_or (value *arg1, value *arg2, tree dest) state::do_or (value *arg1, value *arg2, tree dest)
{ {
@ -460,6 +505,8 @@ state::do_xor (tree arg1, tree arg2, tree dest)
} }
/* Performs XOR operation on given values. The result is stored in dest. */
void void
state::do_xor (value *arg1, value *arg2, tree dest) state::do_xor (value *arg1, value *arg2, tree dest)
{ {
@ -542,6 +589,9 @@ state::do_shift_left (tree arg1, tree arg2, tree dest)
} }
/* Performs shift left operation on given values.
The result is stored in dest. */
void void
state::do_shift_left (value *arg1, value *arg2, tree dest) state::do_shift_left (value *arg1, value *arg2, tree dest)
{ {
@ -570,6 +620,9 @@ state::do_shift_right (tree arg1, tree arg2, tree dest)
} }
/* Performs shift right operation on given values.
The result is stored in dest. */
void void
state::do_shift_right (value *arg1, value *arg2, tree dest) state::do_shift_right (value *arg1, value *arg2, tree dest)
{ {
@ -623,6 +676,8 @@ state::do_add (tree arg1, tree arg2, tree dest)
} }
/* Adds given values. The result is stored in dest. */
void void
state::do_add (value *arg1, value *arg2, tree dest) state::do_add (value *arg1, value *arg2, tree dest)
{ {
@ -672,6 +727,8 @@ state::do_sub (tree arg1, tree arg2, tree dest)
} }
/* Subtracks second value from the first. The result is stored in dest. */
void void
state::do_sub (value *arg1, value *arg2, tree dest) state::do_sub (value *arg1, value *arg2, tree dest)
{ {
@ -735,11 +792,16 @@ state::do_complement (tree arg, tree dest)
} }
/* Does Assignment. */
bool bool
state::do_assign (tree arg, tree dest) state::do_assign (tree arg, tree dest)
{ {
declare_if_needed (dest, tree_to_uhwi (TYPE_SIZE (TREE_TYPE (dest)))); declare_if_needed (dest, tree_to_uhwi (TYPE_SIZE (TREE_TYPE (dest))));
declare_if_needed (arg, var_states.get (dest)->allocated ()); if (TREE_CODE (arg) != INTEGER_CST)
declare_if_needed (arg, tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg))));
else
declare_if_needed (arg, var_states.get (dest)->allocated ());
value *dest_val = var_states.get (dest); value *dest_val = var_states.get (dest);
@ -815,6 +877,7 @@ state::do_assign_pow2 (tree dest, unsigned pow)
(*dest_val)[i] = new bit (0); (*dest_val)[i] = new bit (0);
} }
print_value (dest_val);
return true; return true;
} }
@ -1126,10 +1189,12 @@ state::do_mul (value *arg1, value *arg2, tree dest)
} }
/* Checks whether the given two constant values are equal. */
bool bool
state::check_const_value_equality (value *arg1, value *arg2) state::check_const_value_equality (value *arg1, value *arg2)
{ {
for (size_t i = 1; i < arg1->length (); i++) for (size_t i = 0; i < arg1->length (); i++)
if (as_a<bit *> ((*arg1)[i])->get_val () if (as_a<bit *> ((*arg1)[i])->get_val ()
!= as_a<bit *> ((*arg2)[i])->get_val ()) != as_a<bit *> ((*arg2)[i])->get_val ())
return false; return false;
@ -1137,6 +1202,8 @@ state::check_const_value_equality (value *arg1, value *arg2)
} }
/* Adds EQUAL condition of given variables to state. */
bool bool
state::add_equal_cond (tree arg1, tree arg2) state::add_equal_cond (tree arg1, tree arg2)
{ {
@ -1187,6 +1254,8 @@ state::add_equal_cond (value *arg1, value *arg2)
} }
/* Checks whether the given two constant values are not equal. */
bool bool
state::check_const_value_are_not_equal (value *arg1, value *arg2) state::check_const_value_are_not_equal (value *arg1, value *arg2)
{ {
@ -1198,6 +1267,8 @@ state::check_const_value_are_not_equal (value *arg1, value *arg2)
} }
/* Adds NOT EQUAL condition of given variables to state. */
bool bool
state::add_not_equal_cond (tree arg1, tree arg2) state::add_not_equal_cond (tree arg1, tree arg2)
{ {
@ -1253,6 +1324,9 @@ state::add_not_equal_cond (value *arg1, value *arg2)
} }
/* Checks whether the first given constant value
is greater than the second one. */
bool bool
state::check_const_value_is_greater_than (value *arg1, value *arg2) state::check_const_value_is_greater_than (value *arg1, value *arg2)
{ {
@ -1269,6 +1343,8 @@ state::check_const_value_is_greater_than (value *arg1, value *arg2)
} }
/* Adds GREATER THAN condition of given variables to state. */
bool bool
state::add_greater_than_cond (tree arg1, tree arg2) state::add_greater_than_cond (tree arg1, tree arg2)
{ {
@ -1378,6 +1454,9 @@ state::construct_great_than_cond (value *arg1, value *arg2)
} }
/* Checks whether the first given constant value
is less than the second one. */
bool bool
state::check_const_value_is_less_than (value *arg1, value *arg2) state::check_const_value_is_less_than (value *arg1, value *arg2)
{ {
@ -1394,6 +1473,8 @@ state::check_const_value_is_less_than (value *arg1, value *arg2)
} }
/* Adds LESS THAN condition of given variables to state. */
bool bool
state::add_less_than_cond (tree arg1, tree arg2) state::add_less_than_cond (tree arg1, tree arg2)
{ {
@ -1501,6 +1582,8 @@ state::construct_less_than_cond (value *arg1, value *arg2)
} }
/* Adds GREATER OR EQUAL condition of given variables to state. */
bool bool
state::add_greater_or_equal_cond (tree arg1, tree arg2) state::add_greater_or_equal_cond (tree arg1, tree arg2)
{ {
@ -1553,7 +1636,7 @@ state::add_greater_or_equal_cond (value *arg1, value *arg2)
} }
/* Adds less or equal condition for two sequences of bits. */ /* Adds LESS OR EQUAL condition of given variables to state. */
bool bool
state::add_less_or_equal_cond (tree arg1, tree arg2) state::add_less_or_equal_cond (tree arg1, tree arg2)
@ -1589,6 +1672,8 @@ state::add_less_or_equal_cond (value *arg1, value *arg2)
} }
/* Adds a bool condition to state. */
bool bool
state::add_bool_cond (tree arg) state::add_bool_cond (tree arg)
{ {
@ -1652,7 +1737,7 @@ state::add_binary_cond (tree arg1, tree arg2, binary_cond_func cond_func)
{ {
if (dump_file && (dump_flags & TDF_DETAILS)) if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Sym-Exec: At least one of arguments must be" fprintf (dump_file, "Sym-Exec: At least one of arguments must be"
" declared for adding condition.\n"); " declared for adding the condition.\n");
return false; return false;
} }
@ -1702,8 +1787,8 @@ state::construct_equal_cond (value *arg1, value *arg2)
return nullptr; return nullptr;
} }
/* When some of bits are constants and they differ by value, /* When some bits are constants, and they differ by value,
then we can evalate it to be false. */ then we can evaluate it to be false. */
for (size_t i = 0; i < arg1->length (); i++) for (size_t i = 0; i < arg1->length (); i++)
{ {
if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i]) if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i])
@ -1866,6 +1951,8 @@ value::last ()
} }
/* Make a copy of given bits. */
vec<value_bit *> * vec<value_bit *> *
state::make_copy (vec<value_bit *> *bits) state::make_copy (vec<value_bit *> *bits)
{ {
@ -1946,29 +2033,13 @@ state::do_cast (tree var, tree dest, size_t cast_size)
return true; return true;
} }
/* Get the last 1 bit index. */
size_t
last_set_bit (const value &polynomial)
{
for (size_t i = 0; i < polynomial.length (); ++i)
{
if (as_a<bit *> (polynomial[polynomial.length () - i - 1])->get_val ())
return polynomial.length () - i - 1;
}
return 0;
}
/* Create LFSR value for the reversed CRC. */ /* Create LFSR value for the reversed CRC. */
void void
state::create_reversed_lfsr (value &lfsr, const value &crc, state::create_reversed_lfsr (value &lfsr, const value &crc,
const value &polynomial) const value &polynomial)
{ {
/* Get the minimal byte size to keep the polynomial. size_t size = polynomial.length ();
Ie, if the last 1 bit of the polynomial is at 6 index, size will be 8. */
size_t size = ((last_set_bit (polynomial)/8) + 1) * 8;
if (size == 0)
return;
/* Determine values of all bits, except MSB. */ /* Determine values of all bits, except MSB. */
for (size_t i = 0; i < size - 1; i++) for (size_t i = 0; i < size - 1; i++)
@ -1993,10 +2064,7 @@ void
state::create_forward_lfsr (value &lfsr, const value &crc, state::create_forward_lfsr (value &lfsr, const value &crc,
const value &polynomial) const value &polynomial)
{ {
size_t size = ((last_set_bit (polynomial)/8) + 1) * 8; size_t size = polynomial.length ();
if (size == 0)
return;
/* Determine value of LSB. */ /* Determine value of LSB. */
if (as_a<bit *> (polynomial[0])->get_val ()) if (as_a<bit *> (polynomial[0])->get_val ())
lfsr.push (crc[size - 1]->copy ()); lfsr.push (crc[size - 1]->copy ());
@ -2014,15 +2082,29 @@ state::create_forward_lfsr (value &lfsr, const value &crc,
} }
/* Get the last 1 bit index. */
size_t
last_set_bit (const value &polynomial)
{
for (size_t i = 0; i < polynomial.length (); ++i)
{
if (as_a<bit *> (polynomial[polynomial.length () - i - 1])->get_val ())
return polynomial.length () - i - 1;
}
return 0;
}
/* Create LFSR value. */ /* Create LFSR value. */
value * value *
state::create_lfsr (tree crc, value *polynomial, bool is_bit_forward) state::create_lfsr (tree crc, value *polynomial, bool is_bit_forward)
{ {
/* Check size compatibility */ /* Check size compatibility */
unsigned HOST_WIDE_INT size = polynomial->length (); unsigned HOST_WIDE_INT polynomial_length = polynomial->length ();
unsigned HOST_WIDE_INT crc_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (crc))); unsigned HOST_WIDE_INT crc_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (crc)));
if (crc_size < size) if (crc_size < polynomial_length)
{ {
if (dump_file && (dump_flags & TDF_DETAILS)) if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "LFSR state creation: " fprintf (dump_file, "LFSR state creation: "
@ -2031,14 +2113,30 @@ state::create_lfsr (tree crc, value *polynomial, bool is_bit_forward)
return nullptr; return nullptr;
} }
/* Create vector of symbolic bits for crc. */ /* Get the minimal byte size to keep the polynomial.
value crc_value (size, TYPE_UNSIGNED (TREE_TYPE (crc))); Ie, if the last 1 bit of the polynomial is at 6 index, size will be 8. */
size_t required_polynomial_size = ((last_set_bit (*polynomial)/8) + 1) * 8;
for (unsigned HOST_WIDE_INT i = 0; i < size; i++) /* Polynomial's length actually equals to the CRC variable's size.
crc_value.push (new symbolic_bit (i, crc)); Now we detect only those CRC calculation algorithms, where leading 1 of the
polynomial isn't kept. */
if (required_polynomial_size == 0
|| required_polynomial_size != polynomial_length)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Polynomial's all bits are zeros "
"or the size of the polynomial is uncertain.\n");
return nullptr;
}
/* Create vector of symbolic bits for crc. */
value crc_value (polynomial_length, TYPE_UNSIGNED (TREE_TYPE (crc)));
for (unsigned HOST_WIDE_INT i = 0; i < polynomial_length; i++)
crc_value.push (new symbolic_bit (i, crc));
/* create LFSR vector. */ /* create LFSR vector. */
value *lfsr = new value (size, TYPE_UNSIGNED (TREE_TYPE (crc))); value *lfsr = new value (polynomial_length, TYPE_UNSIGNED (TREE_TYPE (crc)));
/* Calculate values for LFSR. */ /* Calculate values for LFSR. */
if (is_bit_forward) if (is_bit_forward)

View File

@ -90,12 +90,18 @@ class state {
/* Constructs expression trees of equal condition for given values. */ /* Constructs expression trees of equal condition for given values. */
bit_expression *construct_equal_cond (value *arg1, value *arg2); bit_expression *construct_equal_cond (value *arg1, value *arg2);
/* A wrapper for operations on two bits.
Operation and operands are passed as arguments. */
static value_bit *operate_bits (bit_func bit_op, value_bit *bit1, static value_bit *operate_bits (bit_func bit_op, value_bit *bit1,
value_bit *bit2, value_bit **bit3); value_bit *bit2, value_bit **bit3);
/* A wrapper for operations on three bits.
Operation and operands are passed as arguments. */
static value_bit *operate_bits (bit_func3 bit_op, value_bit *bit1, static value_bit *operate_bits (bit_func3 bit_op, value_bit *bit1,
value_bit *bit2, value_bit **bit3); value_bit *bit2, value_bit **bit3);
/* Performs the given operation on passed arguments.
The result is stored in dest. */
template<class func> template<class func>
void operate (value *arg1, value *arg2, value_bit **bit_arg, tree dest, void operate (value *arg1, value *arg2, value_bit **bit_arg, tree dest,
func bit_op); func bit_op);
@ -105,29 +111,39 @@ class state {
bool do_binary_operation (tree arg1, tree arg2, tree dest, bool do_binary_operation (tree arg1, tree arg2, tree dest,
binary_func bin_func); binary_func bin_func);
/* Performs AND operation on given values. The result is stored in dest. */
void do_and (value *arg1, value *arg2, tree dest); void do_and (value *arg1, value *arg2, tree dest);
/* Performs OR operation on given values. The result is stored in dest. */
void do_or (value *arg1, value *arg2, tree dest); void do_or (value *arg1, value *arg2, tree dest);
/* Performs XOR operation on given values. The result is stored in dest. */
void do_xor (value *arg1, value *arg2, tree dest); void do_xor (value *arg1, value *arg2, tree dest);
/* Performs shift right operation on given values.
The result is stored in dest. */
void do_shift_right (value *arg1, value *arg2, tree dest); void do_shift_right (value *arg1, value *arg2, tree dest);
/* Performs shift left operation on given values.
The result is stored in dest. */
void do_shift_left (value *arg1, value *arg2, tree dest); void do_shift_left (value *arg1, value *arg2, tree dest);
/* Adds given values. The result is stored in dest. */
void do_add (value *arg1, value *arg2, tree dest); void do_add (value *arg1, value *arg2, tree dest);
/* Subtracks second value from the first. The result is stored in dest. */
void do_sub (value *arg1, value *arg2, tree dest); void do_sub (value *arg1, value *arg2, tree dest);
/* Casts arg to cast_size size, stores value in dest. */ /* Casts arg to cast_size size, stores value in dest. */
bool do_cast (tree arg, tree dest, size_t cast_size); bool do_cast (tree arg, tree dest, size_t cast_size);
/* Performs AND operation on two values. */ /* Performs AND operation on two bits. */
static value_bit *and_two_bits (value_bit *arg1, value_bit *arg2); static value_bit *and_two_bits (value_bit *arg1, value_bit *arg2);
/* ANDs every bit of the value with var_bit, stroes the result in var1. */ /* ANDs every bit of the value with var_bit, stroes the result in var1. */
void and_number_bit (value *var1, value_bit *var_bit); void and_number_bit (value *var1, value_bit *var_bit);
/* Multiplies given values. The result is stored in dest. */
void do_mul (value *arg1, value *arg2, tree dest); void do_mul (value *arg1, value *arg2, tree dest);
/* Performs AND operation for 2 symbolic_bit operands. */ /* Performs AND operation for 2 symbolic_bit operands. */
@ -197,14 +213,12 @@ class state {
exists or not. */ exists or not. */
bool is_declared (tree var); bool is_declared (tree var);
/* Declares given variable if it has not been declared yet. */
void declare_if_needed (tree var, size_t size); void declare_if_needed (tree var, size_t size);
/* Shifts number left by size of shift_value. */ /* Shifts number left by size of shift_value. */
value *shift_left_by_const (const value *number, size_t shift_value); value *shift_left_by_const (const value *number, size_t shift_value);
/* Checks if all bits of the given value have constant bit type. */
bool is_bit_vector (const value *var);
/* Adds two bits and carry value. /* Adds two bits and carry value.
Resturn result and stores new carry bit in "carry". */ Resturn result and stores new carry bit in "carry". */
static value_bit *full_adder (value_bit *var1, value_bit *var2, static value_bit *full_adder (value_bit *var1, value_bit *var2,
@ -216,17 +230,16 @@ class state {
/* Adds two values, stores the result in the first one. */ /* Adds two values, stores the result in the first one. */
void add_numbers (value *var1, const value *var2); void add_numbers (value *var1, const value *var2);
/* Make a copy of given bits. */
static vec<value_bit *> *make_copy (vec<value_bit *> *bits); static vec<value_bit *> *make_copy (vec<value_bit *> *bits);
/* Create LFSR value for the reversed CRC. */ /* Create LFSR value for the reversed CRC. */
static void static void create_reversed_lfsr (value &lfsr, const value &crc,
create_reversed_lfsr (value &lfsr, const value &crc, const value &polynomial);
const value &polynomial);
/* Create LFSR value for the forward CRC. */ /* Create LFSR value for the forward CRC. */
static void static void create_forward_lfsr (value &lfsr, const value &crc,
create_forward_lfsr (value &lfsr, const value &crc, const value &polynomial);
const value &polynomial);
public: public:
state () = default; state () = default;
@ -238,23 +251,32 @@ class state {
state (const state &s); state (const state &s);
/* Adds the given variable to state. */
bool add_var_state (tree var, value *state); bool add_var_state (tree var, value *state);
/* Remove all states from the states' vector. */
static void remove_states (vec<state *> *states);
/* Remove all states from the states' vector and release the vector. */
static void clear_states (vec<state *> *states); static void clear_states (vec<state *> *states);
void clear_var_states (); void clear_var_states ();
void clear_conditions (); void clear_conditions ();
/* Adds the given condition to the state. */
bool add_condition (bit_expression *cond); bool add_condition (bit_expression *cond);
/* Bulk add the given conditions to the state. */
bool bulk_add_conditions (const hash_set<bit_expression *> &conds); bool bulk_add_conditions (const hash_set<bit_expression *> &conds);
/* Get value of the given variable. */
value *get_value (tree var); value *get_value (tree var);
/* Get the value of the tree, which is in the beginning of the var_states. */ /* Get the value of the tree, which is in the beginning of the var_states. */
value *get_first_value (); value *get_first_value ();
/* Returns the list of conditions in the state. */
const hash_set<bit_expression *> &get_conditions (); const hash_set<bit_expression *> &get_conditions ();
/* Adds a variable with unknown value to state. Such variables are /* Adds a variable with unknown value to state. Such variables are
@ -270,6 +292,9 @@ class state {
/* Prints added conditions. */ /* Prints added conditions. */
void print_conditions (); void print_conditions ();
/* Checks if all bits of the given value have constant bit type. */
static bool is_bit_vector (const value *var);
/* Returns the number represented by the value. */ /* Returns the number represented by the value. */
static unsigned HOST_WIDE_INT static unsigned HOST_WIDE_INT
make_number (const value *var); make_number (const value *var);
@ -283,6 +308,7 @@ class state {
/* Does bit-level OR operation for given variables. */ /* Does bit-level OR operation for given variables. */
bool do_or (tree arg1, tree arg2, tree dest); bool do_or (tree arg1, tree arg2, tree dest);
/* Does Assignment. */
bool do_assign (tree arg, tree dest); bool do_assign (tree arg, tree dest);
/* Assigns pow 2 value. */ /* Assigns pow 2 value. */
@ -306,6 +332,7 @@ class state {
/* Negates given variable. */ /* Negates given variable. */
bool do_complement (tree arg, tree dest); bool do_complement (tree arg, tree dest);
/* Adds EQUAL condition of given variables to state. */
bool add_equal_cond (tree arg1, tree arg2); bool add_equal_cond (tree arg1, tree arg2);
/* Gets the value of *arg1 and stores it in dest. */ /* Gets the value of *arg1 and stores it in dest. */
@ -317,24 +344,36 @@ class state {
/* Perform subtractions on arg1 pointer. */ /* Perform subtractions on arg1 pointer. */
bool do_pointer_diff (tree arg1, tree arg2, tree dest); bool do_pointer_diff (tree arg1, tree arg2, tree dest);
/* Adds NOT EQUAL condition of given variables to state. */
bool add_not_equal_cond (tree arg1, tree arg2); bool add_not_equal_cond (tree arg1, tree arg2);
/* Adds GREATER THAN condition of given variables to state. */
bool add_greater_than_cond (tree arg1, tree arg2); bool add_greater_than_cond (tree arg1, tree arg2);
/* Adds LESS THAN condition of given variables to state. */
bool add_less_than_cond (tree arg1, tree arg2); bool add_less_than_cond (tree arg1, tree arg2);
/* Adds GREATER OR EQUAL condition of given variables to state. */
bool add_greater_or_equal_cond (tree arg1, tree arg2); bool add_greater_or_equal_cond (tree arg1, tree arg2);
/* Adds LESS OR EQUAL condition of given variables to state. */
bool add_less_or_equal_cond (tree arg1, tree arg2); bool add_less_or_equal_cond (tree arg1, tree arg2);
/* Adds a bool condition to state. */
bool add_bool_cond (tree arg); bool add_bool_cond (tree arg);
/* Checks whether the given two constant values are equal. */
static bool check_const_value_equality (value *arg1, value *arg2); static bool check_const_value_equality (value *arg1, value *arg2);
/* Checks whether the given two constant values are not equal. */
static bool check_const_value_are_not_equal (value *arg1, value *arg2); static bool check_const_value_are_not_equal (value *arg1, value *arg2);
/* Checks whether the first given constant value
is greater than the second one. */
static bool check_const_value_is_greater_than (value *arg1, value *arg2); static bool check_const_value_is_greater_than (value *arg1, value *arg2);
/* Checks whether the first given constant value
is less than the second one. */
static bool check_const_value_is_less_than (value *arg1, value *arg2); static bool check_const_value_is_less_than (value *arg1, value *arg2);
static value_bit *complement_bits_with_origin (value_bit *root, tree origin); static value_bit *complement_bits_with_origin (value_bit *root, tree origin);
@ -358,6 +397,9 @@ class state {
size_t min (size_t a, size_t b, size_t c); size_t min (size_t a, size_t b, size_t c);
/* Performs the given operation on passed arguments.
The result is stored in dest. */
template<class func> template<class func>
void void
state::operate (value *arg1, value *arg2, value_bit **bit_arg, tree dest, state::operate (value *arg1, value *arg2, value_bit **bit_arg, tree dest,