Line data Source code
1 : /* State will store states of variables for a function's single execution path.
2 : It will be used for bit-level symbolic execution to determine values of bits
3 : of function's return value and symbolic marked arguments.
4 : Copyright (C) 2022-2026 Free Software Foundation, Inc.
5 : Contributed by Matevos Mehrabyan <matevosmehrabyan@gmail.com>
6 :
7 : This file is part of GCC.
8 :
9 : GCC is free software; you can redistribute it and/or modify it under
10 : the terms of the GNU General Public License as published by the Free
11 : Software Foundation; either version 3, or (at your option) any later
12 : version.
13 :
14 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 : for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with GCC; see the file COPYING3. If not see
21 : <http://www.gnu.org/licenses/>. */
22 :
23 : /* This symbolic executor is designed to handle operations on the bit level.
24 : It can save values of variables on the bit level. For example byte x = 9
25 : would be represented by the bit-vector x = <0, 0, 0, 0, 1, 0, 1, 0> of
26 : size 8. Variables without values will be represented by bit-vectors of
27 : symbolic bits: x = <x[size - 1], ..., x[1], x[0]> where x[i] is the value
28 : of bit i of variable x.
29 :
30 : Operations are also performed on the bit level. For example, for operation
31 : z = x & y
32 : where
33 : x = <x[size - 1], ..., x[1], x[0]>
34 : y = <y[size - 1], ..., y[1], y[0]>
35 : z will have the value
36 : z = <x[size - 1] & y[size - 1], ..., x[1] & y[1], x[0] & y[0]>
37 :
38 : Each bit of variable can be accessed and examined separately if needed.
39 : Moreover, it does basic optimizations in place.
40 : For example, for operation
41 : z = x | y
42 : where
43 : x = <x[size - 1], ..., x[1], x[0]>,
44 : y = <1, ..., 0, 1>,
45 : z will have the value
46 : z = <1, ..., x[1], 1>
47 : as x | 0 == x and x | 1 == 1
48 :
49 : Besides variables, the symbolic executor can also store
50 : conditions on the bit level.
51 : For example, for x == y
52 : It would add {x[size - 1] == y[size - 1], ..., x[1] == y[1], x[0] == y[0]}
53 : conditions.
54 :
55 : For a more complex condition x > y, it would add
56 : {x[size - 1] > y[size - 1] || (x[size - 1] == y[size -1]
57 : && (x[size - 2] > y[size - 2] || (x[size - 2] == y[size - 2]
58 : && ... (x[0] >= y[0])...)}
59 :
60 : The symbolic executor doesn't run by itself. Instead, it must be dictated
61 : what to do. This makes it flexible and allows for various pre- and
62 : post-processing tasks. Developers adding new operation support must consider
63 : that the operation must be represented on the bit level. Because of
64 : this restriction, it may be hard to add support for some operations.
65 :
66 : To use the symbolic executor, you must create a state object. It is the main
67 : object that contains variables as bit-vectors and conditions.
68 : It is the state object that provides operations for symbolic execution.
69 :
70 : If you are going to execute multiple execution paths, you should clone
71 : the state at branching instructions and execute one state for the execution
72 : path where the branching condition evaluates to 'true', and
73 : the other state for the execution path where the branching condition
74 : evaluates to 'false'. Besides that, you should add the corresponding
75 : conditions to states if you need them.
76 :
77 : Variables are stored in the state's 'var_states' field. It maps the tree
78 : object of the variable to its bit-vector. Path conditions are stored in
79 : the 'conditions' field.
80 :
81 : To declare a variable, you should use 'declare_if_needed' method of state.
82 : It declares the variable if it was not previously declared.
83 : 'create_val_for_const' is used for constant declaration.
84 :
85 : The list of supported operations can be found in 'state::do_operation'
86 : method. It calls the corresponding operation based on the specified
87 : tree_code operation. This is the method that you should use to dictate
88 : to the symbolic executor what operations to perform. You can execute the
89 : desired operations explicitly if needed. Variables for participant
90 : operands will be created implicitly if it was not previously declared.
91 : To add conditions to the state, you should use 'state::add_*_cond' methods.
92 :
93 : A sample usage of the symbolic executor:
94 :
95 : // Example.
96 :
97 : unsigned char foo (unsigned char x, unsigned char y)
98 : {
99 : unsigned char D.2352;
100 : unsigned char result;
101 :
102 : result = x & y;
103 : result = result | 9;
104 : if (result == 23) goto <D.2350>; else goto <D.2351>;
105 : <D.2350>:
106 : result = result ^ y;
107 : <D.2351>:
108 : D.2352 = result;
109 : return D.2352;
110 : }
111 :
112 : // Now, we create the initial state and add the variables to it.
113 : state s;
114 : s.declare_if_needed (x, element_precision (x));
115 : s.declare_if_needed (y, element_precision (y));
116 : s.declare_if_needed (d_2352, element_precision (d_2352));
117 : s.declare_if_needed (result, element_precision (result));
118 :
119 : s.do_operation (BIT_AND_EXPR, x, y, result);
120 : s.do_operation (BIT_OR_EXPR, result, 9, result);
121 :
122 : state s2 (s); // We duplicate the state to save values for each branch.
123 : s.add_equal_cond (result, 23);
124 : s2.add_not_equal_cond (result, 23);
125 :
126 : s.do_operation (BIT_XOR_EXPR, result, y, result);
127 : s.do_assign (result, d_2352);
128 : s2.do_assign (result, d_2352);
129 :
130 : // Now, we have variable values for each execution branch, and we can examine
131 : // them to make decisions.
132 :
133 : value * res = s.get_value (result);
134 : if (is_a<bit_expression *> ((*res)[0]))
135 : {
136 : bit_expression * expr = is_a<bit_expression *> ((*res)[0]);
137 : if (is_a<bit *> (expr->get_left ())
138 : && as_a<bit *> (expr->get_left ())->get_val () == 0)
139 : {
140 : ... // Do something.
141 : }
142 : }
143 :
144 : A more general usage would be to iterate over instructions and
145 : call the executor:
146 :
147 : state s;
148 : ...
149 :
150 : for (inst : instructions)
151 : {
152 : enum tree_code rhs_code = gimple_assign_rhs_code (inst);
153 : tree op1 = gimple_assign_rhs1 (gs);
154 : tree op2 = gimple_assign_rhs2 (gs);
155 : tree lhs = gimple_assign_lhs (gs);
156 : s.do_operation (rhs_code, op1, op2, lhs);
157 : ...
158 : }
159 :
160 : */
161 :
162 : #include "sym-exec-state.h"
163 :
164 : /* Returns the minimum of A, B, C. */
165 :
166 38754 : size_t min (size_t a, size_t b, size_t c)
167 : {
168 38754 : size_t min = (a < b ? a : b);
169 38754 : return min < c ? min : c;
170 : }
171 :
172 :
173 : /* Copy constructor for state. It copies all variables and conditions
174 : of the given state. */
175 :
176 18323 : state::state (const state &s)
177 : {
178 196370 : for (auto iter = s.var_states.begin (); iter != s.var_states.end (); ++iter)
179 : {
180 178047 : value val ((*iter).second.length (), (*iter).second.is_unsigned);
181 7416282 : for (size_t i = 0; i < (*iter).second.length (); i++)
182 7060188 : val.push ((*iter).second[i]->copy ());
183 :
184 178047 : var_states.put ((*iter).first, val);
185 178047 : }
186 :
187 30021 : for (auto iter = s.conditions.begin (); iter != s.conditions.end (); ++iter)
188 11698 : conditions.add (as_a<bit_expression *> ((*iter)->copy ()));
189 18323 : }
190 :
191 :
192 : /* Destructor for state. */
193 :
194 24572 : state::~state ()
195 : {
196 24572 : clear_conditions ();
197 24572 : }
198 :
199 :
200 : /* Checks whether state for variable with specified name already
201 : exists or not. */
202 :
203 : bool
204 300494 : state::is_declared (tree var)
205 : {
206 300494 : return var_states.get (var) != NULL;
207 : }
208 :
209 :
210 : /* Declares given variable if it has not been declared yet. */
211 :
212 : void
213 244327 : state::declare_if_needed (tree var, size_t size)
214 : {
215 244327 : if (TREE_CODE (var) != INTEGER_CST && !is_declared (var))
216 : {
217 93332 : make_symbolic (var, size);
218 93332 : if (dump_file && (dump_flags & TDF_DETAILS))
219 : {
220 66368 : fprintf (dump_file,
221 : "Declaring var ");
222 66368 : print_generic_expr (dump_file, var, dump_flags);
223 66368 : fprintf (dump_file,
224 : " with size %zd\n", size);
225 : }
226 : }
227 244327 : }
228 :
229 :
230 : /* Get value of the given variable. */
231 :
232 : value *
233 34079 : state::get_value (tree var)
234 : {
235 34079 : return var_states.get (var);
236 : }
237 :
238 :
239 : /* Get the value of the tree, which is in the beginning of the var_states. */
240 :
241 : value *
242 0 : state::get_first_value ()
243 : {
244 0 : return &(*(var_states.begin ())).second;
245 : }
246 :
247 :
248 : /* Returns the list of conditions in the state. */
249 :
250 : const hash_set<bit_expression *> &
251 23380 : state::get_conditions ()
252 : {
253 23380 : return conditions;
254 : }
255 :
256 :
257 : /* Checks if sizes of arguments and destination are compatible. */
258 :
259 : bool
260 53273 : state::check_args_compatibility (tree arg1, tree arg2, tree dest)
261 : {
262 53273 : if (!(get_var_size (arg1) == get_var_size (dest)
263 2 : || TREE_CODE (arg1) == INTEGER_CST)
264 53275 : || !(get_var_size (arg2) == get_var_size (dest)
265 49009 : || TREE_CODE (arg2) == INTEGER_CST))
266 : {
267 0 : if (dump_file && (dump_flags & TDF_DETAILS))
268 0 : fprintf (dump_file, "Sym-Exec: Incompatible destination "
269 : "and argument sizes.\n");
270 :
271 : return false;
272 : }
273 :
274 : return true;
275 : }
276 :
277 :
278 : /* Creates value for given constant tree. */
279 :
280 : value
281 85906 : state::create_val_for_const (tree var, size_t size)
282 : {
283 85906 : unsigned HOST_WIDE_INT val = TYPE_UNSIGNED (TREE_TYPE (var))
284 85906 : ? tree_to_uhwi (var) : tree_to_shwi (var);
285 85906 : value result (size, TYPE_UNSIGNED (TREE_TYPE (var)));
286 :
287 3380220 : for (size_t i = 0; i < size; i++)
288 : {
289 3208408 : result.push (new bit (val & 1));
290 3208408 : val >>= 1;
291 : }
292 :
293 85906 : return result;
294 : }
295 :
296 :
297 : /* Adds the given variable to state. */
298 :
299 : bool
300 0 : state::add_var_state (tree var, value *vstate)
301 : {
302 0 : size_t size = vstate->length ();
303 0 : value val (size, vstate->is_unsigned);
304 0 : for (size_t i = 0; i < size; i++)
305 0 : val.push ((*vstate)[i]->copy ());
306 :
307 0 : return var_states.put (var, val);
308 0 : }
309 :
310 :
311 : /* Adds the given condition to the state. */
312 :
313 : bool
314 0 : state::add_condition (bit_expression *cond)
315 : {
316 0 : return conditions.add (as_a<bit_expression *> (cond->copy ()));
317 : }
318 :
319 :
320 : /* Bulk add the given conditions to the state. */
321 :
322 : bool
323 0 : state::bulk_add_conditions (const hash_set<bit_expression *> &conds)
324 : {
325 0 : bool status = true;
326 0 : for (auto iter = conds.begin (); iter != conds.end (); ++iter)
327 0 : status &= add_condition (*iter);
328 :
329 0 : return status;
330 : }
331 :
332 :
333 : /* Remove all states from the states' vector. */
334 :
335 : void
336 7006 : state::remove_states (vec<state *> *states)
337 : {
338 19104 : while (!states->is_empty ())
339 : {
340 12098 : delete states->last ();
341 12098 : states->pop ();
342 : }
343 7006 : }
344 :
345 :
346 : /* Remove all states from the states' vector and release the vector. */
347 :
348 : void
349 1514 : state::clear_states (vec<state *> *states)
350 : {
351 1514 : remove_states (states);
352 1514 : states->release ();
353 1514 : }
354 :
355 :
356 : /* Removes all variables added to the state. */
357 :
358 : void
359 0 : state::clear_var_states ()
360 : {
361 0 : var_states.empty ();
362 0 : }
363 :
364 :
365 : /* Removes all conditions added to the state. */
366 :
367 : void
368 24572 : state::clear_conditions ()
369 : {
370 47968 : for (auto iter = conditions.begin (); iter != conditions.end (); ++iter)
371 23396 : delete (*iter);
372 24572 : conditions.empty ();
373 24572 : }
374 :
375 :
376 : /* Performs AND operation for 2 symbolic_bit operands. */
377 :
378 : value_bit *
379 0 : state::and_sym_bits (const value_bit *var1, const value_bit *var2)
380 : {
381 0 : return new bit_and_expression (var1->copy (), var2->copy ());
382 : }
383 :
384 :
385 : /* Performs AND operation for a symbolic_bit and const_bit operands. */
386 :
387 : value_bit *
388 161336 : state::and_var_const (const value_bit *var1, const bit *const_bit)
389 : {
390 161336 : if (const_bit->get_val () == 1)
391 3801 : return var1->copy ();
392 :
393 157535 : return new bit (0);
394 : }
395 :
396 :
397 : /* Performs AND operation for 2 constant bit operands. */
398 :
399 : bit *
400 2463544 : state::and_const_bits (const bit *const_bit1, const bit *const_bit2)
401 : {
402 2463544 : if (const_bit1->get_val () == const_bit2->get_val ())
403 1187331 : return new bit (*const_bit1);
404 :
405 1276213 : return new bit (0);
406 : }
407 :
408 :
409 : /* Performs OR operation for 2 symbolic_bit operands. */
410 :
411 : value_bit *
412 0 : state::or_sym_bits (const value_bit *var1, const value_bit *var2)
413 : {
414 0 : return new bit_or_expression (var1->copy (), var2->copy ());
415 : }
416 :
417 :
418 : /* Performs OR operation for a symbolic_bit and a constant bit operands. */
419 :
420 : value_bit *
421 0 : state::or_var_const (const value_bit *var1, const bit *const_bit)
422 : {
423 0 : if (const_bit->get_val () == 0)
424 0 : return var1->copy ();
425 :
426 0 : return new bit (1);
427 : }
428 :
429 :
430 : /* Performs OR operation for 2 constant bit operands. */
431 :
432 : bit *
433 1216032 : state::or_const_bits (const bit *const_bit1, const bit *const_bit2)
434 : {
435 1216032 : if (const_bit1->get_val () == const_bit2->get_val ())
436 806150 : return new bit (*const_bit1);
437 :
438 409882 : return new bit (1);
439 : }
440 :
441 :
442 : /* Adds an empty state for the given variable. */
443 :
444 : bool
445 397 : state::decl_var (tree var, unsigned size)
446 : {
447 397 : if (is_declared (var))
448 : return false;
449 :
450 397 : value val (size, TYPE_UNSIGNED (TREE_TYPE (var)));
451 9618 : for (unsigned i = 0; i < size; i++)
452 8824 : val.push (nullptr);
453 :
454 397 : return var_states.put (var, val);
455 397 : }
456 :
457 :
458 : /* Returns size of the given variable. */
459 :
460 : unsigned
461 819365 : state::get_var_size (tree var)
462 : {
463 819365 : value *content = var_states.get (var);
464 819365 : if (content == NULL)
465 : return 0;
466 :
467 770354 : return content->allocated ();
468 : }
469 :
470 :
471 : /* Adds a variable with unknown value to state. Such variables are
472 : represented as sequence of symbolic bits. */
473 :
474 : bool
475 93332 : state::make_symbolic (tree var, unsigned size)
476 : {
477 93332 : if (is_declared (var))
478 : return false;
479 :
480 93332 : value val (size, TYPE_UNSIGNED (TREE_TYPE (var)));
481 : /* Initialize each bit of a variable with unknown value. */
482 3802560 : for (size_t i = 0; i < size; i++)
483 3615896 : val.push (new symbolic_bit (i, var));
484 :
485 93332 : return var_states.put (var, val);
486 93332 : }
487 :
488 :
489 : /* Performs AND operation on two bits. */
490 :
491 : value_bit *
492 2624880 : state::and_two_bits (value_bit *arg1, value_bit *arg2)
493 : {
494 2624880 : value_bit *result = nullptr;
495 :
496 5088424 : if (is_a<bit *> (arg1) && is_a<bit *> (arg2))
497 2463544 : result = and_const_bits (as_a<bit *> (arg1), as_a<bit *> (arg2));
498 :
499 161336 : else if (is_a<bit *> (arg1) && (is_a<symbolic_bit *> (arg2)
500 0 : || (is_a<bit_expression *> (arg2))))
501 0 : result = and_var_const (arg2, as_a<bit *> (arg1));
502 :
503 161336 : else if ((is_a<symbolic_bit *> (arg1)
504 430095 : || (is_a<bit_expression *> (arg1))) && is_a<bit *> (arg2))
505 161336 : result = and_var_const (arg1, as_a<bit *> (arg2));
506 :
507 : else
508 0 : result = and_sym_bits (arg1, arg2);
509 :
510 2624880 : return result;
511 : }
512 :
513 :
514 : /* A wrapper for operations on two bits.
515 : Operation and operands are passed as arguments. */
516 :
517 : value_bit *
518 673880 : state::operate_bits (bit_func bit_op, value_bit *bit1, value_bit *bit2,
519 : value_bit **)
520 : {
521 673880 : return (bit_op) (bit1, bit2);
522 : }
523 :
524 :
525 : /* A wrapper for operations on three bits.
526 : Operation and operands are passed as arguments. */
527 :
528 : value_bit *
529 810632 : state::operate_bits (bit_func3 bit_op, value_bit *bit1, value_bit *bit2,
530 : value_bit **bit3)
531 : {
532 810632 : return (bit_op) (bit1, bit2, bit3);
533 : }
534 :
535 :
536 : /* Does preprocessing and postprocessing for expressions with tree operands.
537 : Handles value creation for constant and their removement in the end. */
538 :
539 : bool
540 53273 : state::do_binary_operation (tree arg1, tree arg2, tree dest,
541 : binary_func bin_func)
542 : {
543 53273 : declare_if_needed (dest, element_precision (dest));
544 53273 : declare_if_needed (arg1, var_states.get (dest)->allocated ());
545 53273 : declare_if_needed (arg2, var_states.get (dest)->allocated ());
546 :
547 53273 : if (!check_args_compatibility (arg1, arg2, dest))
548 : return false;
549 :
550 53273 : size_t dest_size = var_states.get (dest)->length ();
551 :
552 53273 : value *arg1_val = var_states.get (arg1);
553 53273 : value arg1_const_val (dest_size, false);
554 53273 : if (arg1_val == NULL && TREE_CODE (arg1) == INTEGER_CST)
555 : {
556 2 : arg1_const_val = create_val_for_const (arg1, dest_size);
557 2 : arg1_val = &arg1_const_val;
558 : }
559 :
560 53273 : value *arg2_val = var_states.get (arg2);
561 53273 : value arg2_const_val (dest_size, false);
562 53273 : if (arg2_val == NULL && TREE_CODE (arg2) == INTEGER_CST)
563 : {
564 49009 : arg2_const_val = create_val_for_const (arg2, dest_size);
565 49009 : arg2_val = &arg2_const_val;
566 : }
567 :
568 53273 : (this->*bin_func) (arg1_val, arg2_val, dest);
569 53273 : print_value (var_states.get (dest));
570 53273 : return true;
571 53273 : }
572 :
573 :
574 : /* Performs AND operation on given values. The result is stored in dest. */
575 :
576 : void
577 4066 : state::do_and (value *arg1, value *arg2, tree dest)
578 : {
579 : /* Creating AND expressions for every bit pair of given arguments
580 : and store them as a new state for given destination. */
581 :
582 4066 : operate (arg1, arg2, nullptr, dest, &state::and_two_bits);
583 4066 : }
584 :
585 :
586 : /* Performs OR operation on two bits. */
587 :
588 : value_bit *
589 1216032 : state::or_two_bits (value_bit *arg1_bit, value_bit *arg2_bit)
590 : {
591 1216032 : value_bit *result = nullptr;
592 :
593 2432064 : if (is_a<bit *> (arg1_bit) && is_a<bit *> (arg2_bit))
594 1216032 : result = or_const_bits (as_a<bit *> (arg1_bit), as_a<bit *> (arg2_bit));
595 :
596 0 : else if (is_a<bit *> (arg1_bit) && (is_a<symbolic_bit *> (arg2_bit)
597 0 : || is_a<bit_expression *> (arg2_bit)))
598 0 : result = or_var_const (arg2_bit, as_a<bit *> (arg1_bit));
599 :
600 0 : else if ((is_a<symbolic_bit *> (arg1_bit)
601 0 : || is_a<bit_expression *> (arg1_bit))
602 0 : && is_a<bit *> (arg2_bit))
603 0 : result = or_var_const (arg1_bit, as_a<bit *> (arg2_bit));
604 :
605 : else
606 0 : result = or_sym_bits (arg1_bit, arg2_bit);
607 :
608 1216032 : return result;
609 : }
610 :
611 :
612 : /* Performs OR operation on given values. The result is stored in dest. */
613 :
614 : void
615 4 : state::do_or (value *arg1, value *arg2, tree dest)
616 : {
617 : /* Creating OR expressions for every bit pair of given arguments
618 : and store them as a new state for given destination. */
619 4 : operate (arg1, arg2, nullptr, dest, &state::or_two_bits);
620 4 : }
621 :
622 :
623 : /* Performs XOR operation on two bits. */
624 :
625 : value_bit *
626 2916737 : state::xor_two_bits (value_bit *arg1_bit, value_bit *arg2_bit)
627 : {
628 2916737 : value_bit *result = nullptr;
629 :
630 5427075 : if (is_a<bit *> (arg1_bit) && is_a<bit *> (arg2_bit))
631 2478866 : result = xor_const_bits (as_a<bit *> (arg1_bit), as_a<bit *> (arg2_bit));
632 :
633 469343 : else if (is_a<bit *> (arg1_bit) && (is_a<symbolic_bit *> (arg2_bit)
634 0 : || is_a<bit_expression *> (arg2_bit)))
635 31472 : result = xor_var_const (arg2_bit, as_a<bit *> (arg1_bit));
636 :
637 406399 : else if ((is_a<symbolic_bit *> (arg1_bit)
638 0 : || is_a<bit_expression *> (arg1_bit))
639 812798 : && is_a<bit *> (arg2_bit))
640 247630 : result = xor_var_const (arg1_bit, as_a<bit *> (arg2_bit));
641 :
642 : else
643 158769 : result = xor_sym_bits (arg1_bit, arg2_bit);
644 :
645 2916737 : return result;
646 : }
647 :
648 :
649 : /* Performs XOR operation on given values. The result is stored in dest. */
650 :
651 : void
652 10467 : state::do_xor (value *arg1, value *arg2, tree dest)
653 : {
654 10467 : operate (arg1, arg2, nullptr, dest, &state::xor_two_bits);
655 10467 : }
656 :
657 :
658 : /* Shifts value right by size of shift_value. */
659 :
660 : value *
661 8023 : state::shift_right_by_const (value *var, size_t shift_value)
662 : {
663 8023 : value *shift_result = new value (var->length (), var->is_unsigned);
664 8023 : if (var->length () <= shift_value)
665 66 : for (size_t i = 0; i < var->length (); i++)
666 64 : shift_result->push (new bit (0));
667 : else
668 : {
669 : size_t i = 0;
670 335724 : for (; i < var->length () - shift_value; ++i)
671 327703 : shift_result->push (((*var)[shift_value + i])->copy ());
672 :
673 34886 : for (; i < var->length (); ++i)
674 27663 : shift_result->push (var->is_unsigned ? new bit (0)
675 798 : : var->last ()->copy ());
676 : }
677 8023 : return shift_result;
678 : }
679 :
680 :
681 : /* Checks if all bits of the given value have constant bit type. */
682 :
683 : bool
684 77542 : state::is_bit_vector (const value *var)
685 : {
686 77542 : if (var == nullptr || !var->exists ())
687 : return false;
688 :
689 2931438 : for (size_t i = 0; i < var->length (); i++)
690 2865594 : if (!(is_a<bit *> ((*var)[i])))
691 : return false;
692 : return true;
693 : }
694 :
695 :
696 : /* Returns the number represented by the value. */
697 :
698 : unsigned HOST_WIDE_INT
699 30029 : state::make_number (const value *var)
700 : {
701 30029 : unsigned HOST_WIDE_INT
702 30029 : number = 0;
703 30029 : int value_size = var->length ();
704 1280925 : for (int i = value_size - 1; i >= 0; i--)
705 : {
706 1250896 : if (is_a<bit *> ((*var)[i]))
707 1250896 : number = (number << 1) | as_a<bit *> ((*var)[i])->get_val ();
708 : else
709 : return 0;
710 : }
711 : return number;
712 : }
713 :
714 :
715 : /* Shift_left operation. Case: var2 is a symbolic value. */
716 :
717 : value_bit *
718 0 : state::shift_left_sym_bits (value_bit *var1, value_bit *var2)
719 : {
720 0 : return new shift_left_expression (var1->copy (), var2->copy ());
721 : }
722 :
723 :
724 : /* Performs shift left operation on given values.
725 : The result is stored in dest. */
726 :
727 : void
728 6490 : state::do_shift_left (value *arg1, value *arg2, tree dest)
729 : {
730 6490 : if (is_bit_vector (arg2))
731 : {
732 6490 : size_t shift_value = make_number (arg2);
733 6490 : value *result = shift_left_by_const (arg1, shift_value);
734 250108 : for (size_t i = 0; i < get_var_size (dest); i++)
735 : {
736 237128 : delete (*var_states.get (dest))[i];
737 237128 : (*var_states.get (dest))[i] = (*result)[i]->copy ();
738 : }
739 6490 : delete result;
740 : }
741 : else
742 0 : operate (arg1, arg2, nullptr, dest, &state::shift_left_sym_bits);
743 6490 : }
744 :
745 :
746 : /* Performs shift right operation on given values.
747 : The result is stored in dest. */
748 :
749 : void
750 8023 : state::do_shift_right (value *arg1, value *arg2, tree dest)
751 : {
752 8023 : if (is_bit_vector (arg2))
753 : {
754 8023 : size_t shift_value = make_number (arg2);
755 8023 : value *result = shift_right_by_const (arg1, shift_value);
756 370678 : for (size_t i = 0; i < get_var_size (dest); i++)
757 : {
758 354632 : delete (*var_states.get (dest))[i];
759 354632 : (*var_states.get (dest))[i] = (*result)[i]->copy ();
760 : }
761 :
762 8023 : delete result;
763 : }
764 : else
765 0 : operate (arg1, arg2, nullptr, dest, &state::shift_right_sym_bits);
766 8023 : }
767 :
768 :
769 : /* Adds two bits and carry value.
770 : Returns result and stores new carry bit in "carry". */
771 :
772 : value_bit *
773 1215928 : state::full_adder (value_bit *var1, value_bit *var2, value_bit **carry)
774 : {
775 1215928 : value_bit *new_carry = and_two_bits (var1, var2);
776 1215928 : value_bit *sum = xor_two_bits (var1, var2);
777 :
778 1215928 : value_bit *result = xor_two_bits (sum, *carry);
779 1215928 : value_bit *sum_and_carry = and_two_bits (sum, *carry);
780 :
781 1215928 : delete *carry;
782 1215928 : delete sum;
783 :
784 1215928 : *carry = or_two_bits (sum_and_carry, new_carry);
785 :
786 1215928 : delete sum_and_carry;
787 1215928 : delete new_carry;
788 :
789 1215928 : return result;
790 : }
791 :
792 :
793 : /* Adds given values. The result is stored in dest. */
794 :
795 : void
796 24217 : state::do_add (value *arg1, value *arg2, tree dest)
797 : {
798 24217 : value_bit *carry = new bit (0);
799 24217 : operate (arg1, arg2, &carry, dest, &state::full_adder);
800 24217 : delete carry;
801 24217 : }
802 :
803 :
804 : /* Returns the additive inverse of the given number. */
805 :
806 : value *
807 12088 : state::additive_inverse (const value *number)
808 : {
809 12088 : value *result = new value (number->length (), number->is_unsigned);
810 12088 : value one (number->length (), number->is_unsigned);
811 :
812 12088 : size_t size = number->length ();
813 12088 : one.push (new bit (1));
814 12088 : result->push (complement_a_bit ((*number)[0]));
815 :
816 417192 : for (size_t i = 1; i < size; i++)
817 : {
818 393016 : one.push (new bit (0));
819 393016 : result->push (complement_a_bit ((*number)[i]));
820 : }
821 :
822 12088 : value_bit *carry = new bit (0);
823 417192 : for (size_t i = 0; i < size; ++i)
824 : {
825 405104 : value_bit *cur_bit = (*result)[i];
826 405104 : (*result)[i] = full_adder (cur_bit, one[i], &carry);
827 405104 : delete cur_bit;
828 : }
829 :
830 12088 : delete carry;
831 24176 : return result;
832 12088 : }
833 :
834 :
835 : /* Subtracks second value from the first. The result is stored in dest. */
836 :
837 : void
838 12088 : state::do_sub (value *arg1, value *arg2, tree dest)
839 : {
840 12088 : value *neg_arg2 = additive_inverse (arg2);
841 12088 : do_add (arg1, neg_arg2, dest);
842 12088 : delete neg_arg2;
843 12088 : }
844 :
845 :
846 : /* Performs complement operation on a bit. */
847 :
848 : value_bit *
849 405104 : state::complement_a_bit (value_bit *var)
850 : {
851 405104 : value_bit *result = nullptr;
852 405104 : if (is_a<bit *> (var))
853 405104 : result = complement_const_bit (as_a<bit *> (var));
854 : else
855 0 : result = complement_sym_bit (var);
856 :
857 405104 : return result;
858 : }
859 :
860 :
861 : /* Negates given variable. */
862 :
863 : bool
864 0 : state::do_complement (tree arg, tree dest)
865 : {
866 0 : declare_if_needed (dest, element_precision (dest));
867 0 : declare_if_needed (arg, var_states.get (dest)->allocated ());
868 :
869 : /* Creating complement expressions for every bit the given argument
870 : and store it as a new state for given destination. */
871 0 : size_t iter_count = min (get_var_size (dest), get_var_size (arg),
872 0 : get_var_size (arg));
873 :
874 0 : size_t i = 0;
875 0 : for (; i < iter_count; i++)
876 : {
877 0 : value_bit *result = complement_a_bit ((*var_states.get (arg))[i]);
878 0 : delete (*var_states.get (dest))[i];
879 0 : (*var_states.get (dest))[i] = result;
880 : }
881 :
882 0 : if (i >= get_var_size (dest))
883 : {
884 0 : print_value (var_states.get (dest));
885 0 : return true;
886 : }
887 :
888 0 : for (; i < get_var_size (dest); i++)
889 : {
890 0 : delete (*var_states.get (dest))[i];
891 0 : bit tmp (0);
892 0 : (*var_states.get (dest))[i] = complement_a_bit (&tmp);
893 0 : }
894 :
895 0 : print_value (var_states.get (dest));
896 0 : return true;
897 : }
898 :
899 :
900 : /* Does Assignment. */
901 :
902 : bool
903 30168 : state::do_assign (tree arg, tree dest)
904 : {
905 30168 : declare_if_needed (dest, element_precision (dest));
906 30168 : if (TREE_CODE (arg) != INTEGER_CST)
907 17445 : declare_if_needed (arg, element_precision (arg));
908 : else
909 12723 : declare_if_needed (arg, var_states.get (dest)->allocated ());
910 :
911 30168 : value *dest_val = var_states.get (dest);
912 :
913 : /* If the argument is already defined, then we must just copy its bits. */
914 30168 : if (auto arg_val = var_states.get (arg))
915 : {
916 722205 : for (size_t i = 0; i < dest_val->length (); i++)
917 : {
918 704760 : value_bit *new_val = nullptr;
919 704760 : if (i < arg_val->length ())
920 643008 : new_val = (*arg_val)[i]->copy ();
921 61752 : else if (TYPE_UNSIGNED (TREE_TYPE (arg)))
922 61596 : new_val = new bit (0);
923 : else
924 156 : new_val = (*arg_val)[arg_val->length () - 1]->copy ();
925 :
926 704760 : delete (*dest_val)[i];
927 704760 : (*dest_val)[i] = new_val;
928 : }
929 : }
930 : /* If the argument is a constant, we must save it as sequence of bits. */
931 12723 : else if (TREE_CODE (arg) == INTEGER_CST)
932 : {
933 12723 : value arg_val
934 12723 : = create_val_for_const (arg, dest_val->length ());
935 450886 : for (size_t i = 0; i < dest_val->length (); i++)
936 : {
937 425440 : delete (*dest_val)[i];
938 425440 : (*dest_val)[i] = arg_val[i]->copy ();
939 : }
940 12723 : }
941 : else
942 : {
943 0 : if (dump_file && (dump_flags & TDF_DETAILS))
944 0 : fprintf (dump_file, "Sym-Exec: Unsupported assignment"
945 : " for given argument.\n");
946 :
947 : return false;
948 : }
949 :
950 30168 : print_value (var_states.get (dest));
951 30168 : return true;
952 : }
953 :
954 :
955 : /* Assigns pow 2 value. */
956 :
957 : bool
958 397 : state::do_assign_pow2 (tree dest, unsigned pow)
959 : {
960 397 : value *dest_val = var_states.get (dest);
961 397 : unsigned dest_size = dest_val ? dest_val->allocated ()
962 397 : : TYPE_PRECISION (TREE_TYPE (dest));
963 397 : if (pow > dest_size)
964 : {
965 0 : if (dump_file && (dump_flags & TDF_DETAILS))
966 0 : fprintf (dump_file, "Sym-Exec: pow %u of 2 won't fit in"
967 : " specified destination\n", pow);
968 : return false;
969 : }
970 :
971 397 : if (!dest_val)
972 : {
973 397 : decl_var (dest, TYPE_PRECISION (TREE_TYPE (dest)));
974 397 : dest_val = var_states.get (dest);
975 : }
976 : else
977 0 : dest_val->free_bits ();
978 :
979 9221 : for (unsigned i = 0; i < dest_val->length (); i++)
980 : {
981 8824 : if (i == pow)
982 397 : (*dest_val)[i] = new bit (1);
983 : else
984 8427 : (*dest_val)[i] = new bit (0);
985 : }
986 :
987 397 : print_value (dest_val);
988 397 : return true;
989 : }
990 :
991 :
992 : /* Performs NOT operation for constant bit. */
993 :
994 : bit *
995 405104 : state::complement_const_bit (const bit *const_bit)
996 : {
997 405104 : return new bit (1u ^ const_bit->get_val ());
998 : }
999 :
1000 :
1001 : /* Performs NOT operation for symbolic_bit. */
1002 :
1003 : value_bit *
1004 0 : state::complement_sym_bit (const value_bit *var)
1005 : {
1006 0 : return new bit_complement_expression (var->copy ());
1007 : }
1008 :
1009 :
1010 : /* Performs XOR operation for 2 symbolic_bit operands. */
1011 :
1012 : value_bit *
1013 158769 : state::xor_sym_bits (const value_bit *var1, const value_bit *var2)
1014 : {
1015 158769 : value_bit *var2_copy = var2->copy ();
1016 158769 : bit_expression *node2_with_const_child = nullptr;
1017 158769 : bit_expression *parent_of_node2_with_child = nullptr;
1018 158769 : get_parent_with_const_child (var2_copy, node2_with_const_child,
1019 : parent_of_node2_with_child);
1020 :
1021 158769 : if (node2_with_const_child != nullptr)
1022 : {
1023 0 : value_bit *var1_copy = var1->copy ();
1024 0 : bit_expression *node1_with_const_child = nullptr;
1025 0 : bit_expression *parent_of_node1_with_child = nullptr;
1026 0 : get_parent_with_const_child (var1_copy, node1_with_const_child,
1027 : parent_of_node1_with_child);
1028 :
1029 : /* If both subtrees have constant bit nodes,
1030 : we can merge them together. */
1031 0 : if (node1_with_const_child != nullptr)
1032 : {
1033 0 : value_bit *var1_reformed = nullptr;
1034 0 : value_bit *var2_reformed = nullptr;
1035 :
1036 : /* If var1's const bit is in its left subtree. */
1037 0 : value_bit *var1_left = node1_with_const_child->get_left ();
1038 0 : if (var1_left != nullptr && is_a<bit *> (var1_left))
1039 : {
1040 0 : var1_reformed = node1_with_const_child->get_right ()->copy ();
1041 0 : value_bit *var2_left = node2_with_const_child->get_left ();
1042 :
1043 : /* If var2's const bit is in its left subtree. */
1044 0 : if (var2_left != nullptr && is_a<bit *> (var2_left))
1045 0 : var2_reformed = node2_with_const_child->get_right ()->copy ();
1046 : else /* Var2's const bit is in its right subtree. */
1047 0 : var2_reformed = node2_with_const_child->get_left ()->copy ();
1048 : }
1049 : else /* Var1's const bit is in its right subtree. */
1050 : {
1051 0 : var1_reformed = node1_with_const_child->get_left ()->copy ();
1052 0 : value_bit *var2_left = node2_with_const_child->get_left ();
1053 :
1054 : /* If var2's const bit is in its left subtree. */
1055 0 : if (var2_left != nullptr && is_a<bit *> (var2_left))
1056 0 : var2_reformed = node2_with_const_child->get_right ()->copy ();
1057 : else /* Var2's const bit is in its right subtree. */
1058 0 : var2_reformed = node2_with_const_child->get_left ()->copy ();
1059 : }
1060 :
1061 0 : if (parent_of_node1_with_child)
1062 : {
1063 0 : parent_of_node1_with_child->get_left ()
1064 0 : == node1_with_const_child
1065 0 : ? parent_of_node1_with_child->set_left (var1_reformed)
1066 0 : : parent_of_node1_with_child->set_right (var1_reformed);
1067 0 : delete node1_with_const_child;
1068 : }
1069 : else
1070 : {
1071 0 : delete var1_copy;
1072 : var1_copy = var1_reformed;
1073 : }
1074 :
1075 0 : if (parent_of_node2_with_child)
1076 : {
1077 0 : parent_of_node2_with_child->get_left ()
1078 0 : == node2_with_const_child
1079 0 : ? parent_of_node2_with_child->set_left (var2_reformed)
1080 0 : : parent_of_node2_with_child->set_right (var2_reformed);
1081 0 : delete node2_with_const_child;
1082 : }
1083 : else
1084 : {
1085 0 : delete var2_copy;
1086 : var2_copy = var2_reformed;
1087 : }
1088 :
1089 0 : return new bit_xor_expression (var1_copy, var2_copy);
1090 : }
1091 0 : delete var1_copy;
1092 : }
1093 :
1094 158769 : delete var2_copy;
1095 158769 : return new bit_xor_expression (var1->copy (), var2->copy ());
1096 : }
1097 :
1098 :
1099 : /* Performs XOR operation for 2 constant bit operands. */
1100 :
1101 : bit *
1102 2478866 : state::xor_const_bits (const bit *const_bit1, const bit *const_bit2)
1103 : {
1104 2478866 : return new bit (const_bit1->get_val () ^ const_bit2->get_val ());
1105 : }
1106 :
1107 :
1108 : /* Performs XOR operation for a symbolic_bit and const_bit operands. */
1109 :
1110 : value_bit *
1111 279102 : state::xor_var_const (const value_bit *var, const bit *const_bit)
1112 : {
1113 279102 : if (const_bit->get_val () == 0)
1114 174365 : return var->copy ();
1115 :
1116 104737 : value_bit *var_copy = var->copy ();
1117 104737 : bit_expression *node_with_const_child = nullptr;
1118 104737 : bit_expression *tmp = nullptr;
1119 104737 : get_parent_with_const_child (var_copy, node_with_const_child, tmp);
1120 :
1121 104737 : if (node_with_const_child != nullptr)
1122 : {
1123 0 : value_bit *left = node_with_const_child->get_left ();
1124 0 : if (left != nullptr && is_a<bit *> (left))
1125 : {
1126 0 : bit *new_left = xor_const_bits (as_a<bit *> (left), const_bit);
1127 0 : delete left;
1128 0 : node_with_const_child->set_left (new_left);
1129 : }
1130 : else
1131 : {
1132 0 : value_bit *right = node_with_const_child->get_right ();
1133 0 : bit *new_right = xor_const_bits (as_a<bit *> (right), const_bit);
1134 0 : delete right;
1135 0 : node_with_const_child->set_right (new_right);
1136 : }
1137 : return var_copy;
1138 : }
1139 :
1140 104737 : delete var_copy;
1141 104737 : return new bit_xor_expression (var->copy (), const_bit->copy ());
1142 : }
1143 :
1144 :
1145 : /* Return node which has a const bit child. Traversal is done based
1146 : on safe branching. */
1147 :
1148 : void
1149 263506 : state::get_parent_with_const_child (value_bit *root, bit_expression *&parent,
1150 : bit_expression *&parent_of_parent)
1151 : {
1152 263506 : parent_of_parent = nullptr;
1153 263506 : parent = nullptr;
1154 :
1155 263506 : if (!is_a<bit_expression *> (root))
1156 263506 : return;
1157 :
1158 0 : bit_expression *expr_root = as_a<bit_expression *> (root);
1159 0 : hash_set < bit_expression * > nodes_to_consider;
1160 0 : nodes_to_consider.add (expr_root);
1161 :
1162 0 : hash_map < bit_expression * , bit_expression * > node_to_parent;
1163 0 : node_to_parent.put (expr_root, nullptr);
1164 :
1165 : /* Traversing expression tree:
1166 : considering only commutative expression nodes. */
1167 0 : while (!nodes_to_consider.is_empty ())
1168 : {
1169 0 : bit_expression *cur_element = *nodes_to_consider.begin ();
1170 0 : nodes_to_consider.remove (cur_element);
1171 :
1172 0 : value_bit *left = cur_element->get_left ();
1173 0 : value_bit *right = cur_element->get_right ();
1174 :
1175 0 : if ((left != nullptr && is_a<bit *> (left))
1176 0 : || (right != nullptr && is_a<bit *> (right)))
1177 : {
1178 0 : parent = cur_element;
1179 0 : parent_of_parent = *node_to_parent.get (cur_element);
1180 : }
1181 :
1182 0 : if (left != nullptr && is_a<bit_xor_expression *> (left))
1183 : {
1184 0 : nodes_to_consider.add (as_a<bit_expression *> (left));
1185 0 : node_to_parent.put (as_a<bit_expression *> (left), cur_element);
1186 : }
1187 :
1188 0 : if (right != nullptr && is_a<bit_xor_expression *> (right))
1189 : {
1190 0 : nodes_to_consider.add (as_a<bit_expression *> (right));
1191 0 : node_to_parent.put (as_a<bit_expression *> (right), cur_element);
1192 : }
1193 : }
1194 0 : }
1195 :
1196 :
1197 : /* Shifts number left by size of shift_value. */
1198 :
1199 : value *
1200 6682 : state::shift_left_by_const (const value *number, size_t shift_value)
1201 : {
1202 6682 : value *shift_result = new value (number->length (), number->is_unsigned);
1203 6682 : if (number->length () <= shift_value)
1204 0 : for (size_t i = 0; i < number->length (); i++)
1205 0 : shift_result->push (new bit (0));
1206 :
1207 : else
1208 : {
1209 : size_t i = 0;
1210 13364 : for (; i < shift_value; ++i)
1211 6682 : shift_result->push (new bit (0));
1212 243272 : for (size_t j = 0; i < number->length (); ++i, j++)
1213 236590 : shift_result->push (((*number)[j])->copy ());
1214 : }
1215 6682 : return shift_result;
1216 : }
1217 :
1218 :
1219 : /* Shift_right operation. Case: var2 is a symbolic value. */
1220 :
1221 : value_bit *
1222 0 : state::shift_right_sym_bits (value_bit *var1, value_bit *var2)
1223 : {
1224 0 : return new shift_right_expression (var1->copy (), var2->copy ());
1225 : }
1226 :
1227 :
1228 : /* Adds two values, stores the result in the first one. */
1229 :
1230 : void
1231 6 : state::add_numbers (value *var1, const value *var2)
1232 : {
1233 6 : value_bit *carry = new bit (0);
1234 198 : for (unsigned i = 0; i < var1->length (); i++)
1235 : {
1236 192 : value_bit *temp = (*var1)[i];
1237 192 : (*var1)[i] = full_adder ((*var1)[i], (*var2)[i], &carry);
1238 192 : delete temp;
1239 : }
1240 6 : delete carry;
1241 6 : }
1242 :
1243 :
1244 : /* ANDs every bit of the vector with var_bit, stores the result in var1. */
1245 :
1246 : void
1247 0 : state::and_number_bit (value *var1, value_bit *var_bit)
1248 : {
1249 0 : for (unsigned i = 0; i < var1->length (); i++)
1250 : {
1251 0 : value_bit *tmp = (*var1)[i];
1252 0 : (*var1)[i] = and_two_bits ((*var1)[i], var_bit);
1253 0 : delete tmp;
1254 : }
1255 :
1256 0 : }
1257 :
1258 :
1259 : /* Multiplies given values. The result is stored in dest. */
1260 :
1261 : void
1262 6 : state::do_mul (value *arg1, value *arg2, tree dest)
1263 : {
1264 6 : value *shifted = new value (*arg1);
1265 6 : value *dest_val = var_states.get (dest);
1266 :
1267 204 : for (unsigned i = 0; i < dest_val->length (); i++)
1268 : {
1269 192 : delete (*dest_val)[i];
1270 192 : (*dest_val)[i] = new bit (0);
1271 : }
1272 :
1273 198 : for (unsigned i = arg2->length (); i != 0; --i)
1274 : {
1275 192 : if (is_a<bit *> ((*arg2)[i - 1])
1276 192 : && as_a<bit *> ((*arg2)[i - 1])->get_val () != 0)
1277 6 : add_numbers (dest_val, shifted);
1278 186 : else if (is_a<symbolic_bit *> ((*arg2)[i - 1]))
1279 : {
1280 0 : and_number_bit (shifted, as_a<symbolic_bit *> ((*arg2)[i - 1]));
1281 0 : add_numbers (dest_val, shifted);
1282 : }
1283 :
1284 192 : value *temp = shifted;
1285 192 : shifted = shift_left_by_const (shifted, 1);
1286 192 : delete temp;
1287 : }
1288 6 : delete shifted;
1289 6 : }
1290 :
1291 :
1292 : /* Checks whether the given two constant values are equal. */
1293 :
1294 : bool
1295 11050 : state::check_const_value_equality (value *arg1, value *arg2)
1296 : {
1297 381250 : for (size_t i = 0; i < arg1->length (); i++)
1298 740492 : if (as_a<bit *> ((*arg1)[i])->get_val ()
1299 370246 : != as_a<bit *> ((*arg2)[i])->get_val ())
1300 : return false;
1301 : return true;
1302 : }
1303 :
1304 :
1305 : /* Adds EQUAL condition of given variables to state. */
1306 :
1307 : bool
1308 3773 : state::add_equal_cond (tree arg1, tree arg2)
1309 : {
1310 3773 : return add_binary_cond (arg1, arg2, &state::add_equal_cond);
1311 : }
1312 :
1313 :
1314 : /* Adds equality condition for two values. */
1315 :
1316 : void
1317 3773 : state::add_equal_cond (value *arg1, value *arg2)
1318 : {
1319 :
1320 : /* If both arguments are constants then we can evaluate it. */
1321 3773 : if (is_bit_vector (arg1) && is_bit_vector (arg2))
1322 : {
1323 52 : bool result = check_const_value_equality (arg1, arg2);
1324 52 : last_cond_status = result ? condition_status::CS_TRUE
1325 : : condition_status::CS_FALSE;
1326 52 : return;
1327 : }
1328 :
1329 : /* When some of bits are constants and they differ by value,
1330 : then we can evaluate it to be false. */
1331 185857 : for (size_t i = 0; i < arg1->length (); i++)
1332 : {
1333 360551 : if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i])
1334 538966 : && as_a<bit *> ((*arg1)[i])->get_val ()
1335 178415 : != as_a<bit *> ((*arg2)[i])->get_val ())
1336 : {
1337 0 : last_cond_status = condition_status::CS_FALSE;
1338 0 : return;
1339 : }
1340 : }
1341 :
1342 185857 : for (size_t i = 0; i < arg1->length (); i++)
1343 : {
1344 : /* If there is a constant bit pair, then they are equal
1345 : as we checked not equality above. */
1346 182136 : if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i]))
1347 178415 : continue;
1348 :
1349 7442 : conditions.add (new bit_condition ((*arg1)[i]->copy (),
1350 7442 : (*arg2)[i]->copy (),
1351 7442 : EQ_EXPR));
1352 : }
1353 3721 : last_cond_status = condition_status::CS_SYM;
1354 : }
1355 :
1356 :
1357 : /* Checks whether the given two constant values are not equal. */
1358 :
1359 : bool
1360 12291 : state::check_const_value_are_not_equal (value *arg1, value *arg2)
1361 : {
1362 44113 : for (size_t i = 0; i < arg1->length (); i++)
1363 86808 : if (as_a<bit *> ((*arg1)[i])->get_val ()
1364 43404 : != as_a<bit *> ((*arg2)[i])->get_val ())
1365 : return true;
1366 : return false;
1367 : }
1368 :
1369 :
1370 : /* Adds NOT EQUAL condition of given variables to state. */
1371 :
1372 : bool
1373 16012 : state::add_not_equal_cond (tree arg1, tree arg2)
1374 : {
1375 16012 : return add_binary_cond (arg1, arg2, &state::add_not_equal_cond);
1376 : }
1377 :
1378 :
1379 : /* Adds not equal condition for two values. */
1380 :
1381 : void
1382 16012 : state::add_not_equal_cond (value *arg1, value *arg2)
1383 : {
1384 16012 : if (is_bit_vector (arg1) && is_bit_vector (arg2))
1385 : {
1386 12291 : bool result = check_const_value_are_not_equal (arg1, arg2);
1387 12291 : last_cond_status = result ? condition_status::CS_TRUE
1388 : : condition_status::CS_FALSE;
1389 12291 : return;
1390 : }
1391 :
1392 : /* When some of bits are constants and they differ by value,
1393 : then we can evaluate it to be true. */
1394 185857 : for (size_t i = 0; i < arg1->length (); i++)
1395 : {
1396 360551 : if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i])
1397 538966 : && as_a<bit *> ((*arg1)[i])->get_val ()
1398 178415 : != as_a<bit *> ((*arg2)[i])->get_val ())
1399 : {
1400 0 : last_cond_status = condition_status::CS_TRUE;
1401 0 : return;
1402 : }
1403 : }
1404 :
1405 3721 : bit_expression *prev = nullptr;
1406 185857 : for (size_t i = 0; i < arg1->length (); i++)
1407 : {
1408 : /* If there is a constant bit pair, then they are equal
1409 : as we checked not equality above. */
1410 182136 : if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i]))
1411 178415 : continue;
1412 :
1413 3721 : bit_condition *new_cond = new bit_condition ((*arg1)[i]->copy (),
1414 7442 : (*arg2)[i]->copy (),
1415 7442 : NE_EXPR);
1416 3721 : if (prev)
1417 0 : prev = new bit_or_expression (prev, new_cond);
1418 : else
1419 3721 : prev = new_cond;
1420 : }
1421 :
1422 3721 : last_cond_status = condition_status::CS_SYM;
1423 3721 : conditions.add (prev);
1424 : }
1425 :
1426 :
1427 : /* Checks whether the first given constant value
1428 : is greater than the second one. */
1429 :
1430 : bool
1431 0 : state::check_const_value_is_greater_than (value *arg1, value *arg2)
1432 : {
1433 0 : for (int i = arg1->length () - 1; i >= 0; i--)
1434 : {
1435 0 : if (as_a<bit *> ((*arg1)[i])->get_val ()
1436 0 : > as_a<bit *> ((*arg2)[i])->get_val ())
1437 : return true;
1438 0 : else if (as_a<bit *> ((*arg1)[i])->get_val ()
1439 0 : < as_a<bit *> ((*arg2)[i])->get_val ())
1440 : return false;
1441 : }
1442 : return false;
1443 : }
1444 :
1445 :
1446 : /* Adds GREATER THAN condition of given variables to state. */
1447 :
1448 : bool
1449 0 : state::add_greater_than_cond (tree arg1, tree arg2)
1450 : {
1451 0 : return add_binary_cond (arg1, arg2, &state::add_greater_than_cond);
1452 : }
1453 :
1454 :
1455 : /* Adds greater than condition for two values. */
1456 :
1457 : void
1458 0 : state::add_greater_than_cond (value *arg1, value *arg2)
1459 : {
1460 0 : if (is_bit_vector (arg1) && is_bit_vector (arg2))
1461 : {
1462 0 : bool result = check_const_value_is_greater_than (arg1, arg2);
1463 0 : last_cond_status = result ? condition_status::CS_TRUE
1464 : : condition_status::CS_FALSE;
1465 0 : return;
1466 : }
1467 :
1468 0 : if (is_bit_vector (arg2) && is_a<bit *> (arg1->last ())
1469 0 : && make_number (arg2) == 0 && !arg1->is_unsigned)
1470 : {
1471 0 : if (as_a<bit *> (arg1->last ())->get_val () == 1)
1472 0 : last_cond_status = condition_status::CS_FALSE;
1473 : else
1474 : {
1475 0 : for (size_t i = 0; i < arg1->length (); i++)
1476 0 : if (is_a<bit *> ((*arg1)[i])
1477 0 : && as_a<bit *> ((*arg1)[i])->get_val ())
1478 : {
1479 0 : last_cond_status = condition_status::CS_TRUE;
1480 0 : return;
1481 : }
1482 : }
1483 : }
1484 :
1485 0 : bit_expression *gt_cond = construct_great_than_cond (arg1, arg2);
1486 0 : if (gt_cond)
1487 : {
1488 : /* Otherwise its status is already set. */
1489 0 : last_cond_status = condition_status::CS_SYM;
1490 0 : conditions.add (gt_cond);
1491 : }
1492 : }
1493 :
1494 :
1495 : /* Constructs expression trees of greater than condition for given values. */
1496 :
1497 : bit_expression *
1498 0 : state::construct_great_than_cond (value *arg1, value *arg2)
1499 : {
1500 0 : bit_expression *prev = nullptr;
1501 0 : int i = arg1->length () - 1;
1502 0 : for (; i >= 0; i--)
1503 : {
1504 0 : if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i]))
1505 : {
1506 0 : if (as_a<bit *> ((*arg1)[i])->get_val ()
1507 0 : > as_a<bit *> ((*arg2)[i])->get_val ())
1508 : {
1509 0 : if (!prev)
1510 0 : last_cond_status = condition_status::CS_TRUE;
1511 : return prev;
1512 : }
1513 0 : else if (as_a<bit *> ((*arg1)[i])->get_val ()
1514 0 : < as_a<bit *> ((*arg2)[i])->get_val ())
1515 : {
1516 0 : if (prev)
1517 : {
1518 0 : bit_expression *ret_val
1519 0 : = as_a<bit_expression *> (prev->get_left ()->copy ());
1520 0 : delete prev;
1521 0 : return ret_val;
1522 : }
1523 : else
1524 : {
1525 0 : last_cond_status = condition_status::CS_FALSE;
1526 0 : return nullptr;
1527 : }
1528 : }
1529 : }
1530 : else
1531 : {
1532 0 : bit_condition *gt_cond
1533 0 : = new bit_condition ((*arg1)[i]->copy (), (*arg2)[i]->copy (),
1534 0 : GT_EXPR);
1535 0 : bit_expression *expr = nullptr;
1536 0 : if (i)
1537 : {
1538 0 : bit_condition *eq_cond
1539 0 : = new bit_condition ((*arg1)[i]->copy (), (*arg2)[i]->copy (),
1540 0 : EQ_EXPR);
1541 0 : expr = new bit_or_expression (gt_cond, eq_cond);
1542 : }
1543 : else
1544 : expr = gt_cond;
1545 :
1546 0 : if (prev)
1547 0 : prev = new bit_and_expression (expr, prev);
1548 : else
1549 : prev = expr;
1550 : }
1551 : }
1552 :
1553 : return prev;
1554 : }
1555 :
1556 :
1557 : /* Checks whether the first given constant value
1558 : is less than the second one. */
1559 :
1560 : bool
1561 0 : state::check_const_value_is_less_than (value *arg1, value *arg2)
1562 : {
1563 0 : for (int i = arg1->length () - 1; i >= 0; i--)
1564 : {
1565 0 : if (as_a<bit *> ((*arg1)[i])->get_val ()
1566 0 : < as_a<bit *> ((*arg2)[i])->get_val ())
1567 : return true;
1568 0 : else if (as_a<bit *> ((*arg1)[i])->get_val ()
1569 0 : > as_a<bit *> ((*arg2)[i])->get_val ())
1570 : return false;
1571 : }
1572 : return false;
1573 : }
1574 :
1575 :
1576 : /* Adds LESS THAN condition of given variables to state. */
1577 :
1578 : bool
1579 2259 : state::add_less_than_cond (tree arg1, tree arg2)
1580 : {
1581 2259 : return add_binary_cond (arg1, arg2, &state::add_less_than_cond);
1582 : }
1583 :
1584 :
1585 : /* Adds less than condition for two values. */
1586 :
1587 : void
1588 2259 : state::add_less_than_cond (value *arg1, value *arg2)
1589 : {
1590 2390 : if (is_bit_vector (arg1) && is_bit_vector (arg2)
1591 2390 : && (make_number (arg2) != 0 || arg1->is_unsigned))
1592 : {
1593 0 : bool result = check_const_value_is_less_than (arg1, arg2);
1594 0 : last_cond_status = result ? condition_status::CS_TRUE
1595 : : condition_status::CS_FALSE;
1596 2259 : return;
1597 : }
1598 :
1599 2259 : last_cond_status = condition_status::CS_SYM;
1600 2259 : if (is_bit_vector (arg2) && make_number (arg2) == 0 && !arg1->is_unsigned)
1601 : {
1602 2259 : if (is_a<bit *> (arg1->last ()))
1603 : {
1604 131 : if (as_a<bit *> (arg1->last ())->get_val () == 1)
1605 128 : last_cond_status = condition_status::CS_TRUE;
1606 : else
1607 3 : last_cond_status = condition_status::CS_FALSE;
1608 : }
1609 : else
1610 4256 : conditions.add (new bit_condition (arg1->last ()->copy (), new bit (1),
1611 4256 : EQ_EXPR));
1612 :
1613 : return;
1614 : }
1615 :
1616 0 : bit_expression *lt_cond = construct_less_than_cond (arg1, arg2);
1617 0 : if (lt_cond)
1618 : /* Otherwise its status is already set. */
1619 0 : conditions.add (lt_cond);
1620 : }
1621 :
1622 :
1623 : /* Constructs expression trees of less than condition for given values. */
1624 :
1625 : bit_expression *
1626 0 : state::construct_less_than_cond (value *arg1, value *arg2)
1627 : {
1628 0 : bit_expression *prev = nullptr;
1629 0 : int i = arg1->length () - 1;
1630 0 : for (; i >= 0; i--)
1631 : {
1632 0 : if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i]))
1633 : {
1634 0 : if (as_a<bit *> ((*arg1)[i])->get_val ()
1635 0 : < as_a<bit *> ((*arg2)[i])->get_val ())
1636 : {
1637 0 : if (!prev)
1638 0 : last_cond_status = condition_status::CS_TRUE;
1639 : return prev;
1640 : }
1641 0 : else if (as_a<bit *> ((*arg1)[i])->get_val ()
1642 0 : > as_a<bit *> ((*arg2)[i])->get_val ())
1643 : {
1644 0 : if (prev)
1645 : {
1646 0 : bit_expression *ret_val
1647 0 : = as_a<bit_expression *> (prev->get_left ()->copy ());
1648 0 : delete prev;
1649 0 : return ret_val;
1650 : }
1651 : else
1652 : {
1653 0 : last_cond_status = condition_status::CS_FALSE;
1654 0 : return nullptr;
1655 : }
1656 : }
1657 : }
1658 : else
1659 : {
1660 0 : bit_condition *lt_cond
1661 0 : = new bit_condition ((*arg1)[i]->copy (), (*arg2)[i]->copy (),
1662 0 : LT_EXPR);
1663 0 : bit_expression *expr = nullptr;
1664 0 : if (i)
1665 : {
1666 0 : bit_condition *eq_cond
1667 0 : = new bit_condition ((*arg1)[i]->copy (), (*arg2)[i]->copy (),
1668 0 : EQ_EXPR);
1669 0 : expr = new bit_or_expression (lt_cond, eq_cond);
1670 : }
1671 : else
1672 : expr = lt_cond;
1673 :
1674 0 : if (prev)
1675 0 : prev = new bit_and_expression (expr, prev);
1676 : else
1677 : prev = expr;
1678 : }
1679 : }
1680 :
1681 : return prev;
1682 : }
1683 :
1684 :
1685 : /* Adds GREATER OR EQUAL condition of given variables to state. */
1686 :
1687 : bool
1688 2128 : state::add_greater_or_equal_cond (tree arg1, tree arg2)
1689 : {
1690 2128 : return add_binary_cond (arg1, arg2, &state::add_greater_or_equal_cond);
1691 : }
1692 :
1693 :
1694 : /* Adds greater or equal condition for two values. */
1695 :
1696 : void
1697 2128 : state::add_greater_or_equal_cond (value *arg1, value *arg2)
1698 : {
1699 2128 : if (is_bit_vector (arg1) && is_bit_vector (arg2)
1700 2128 : && (make_number (arg2) != 0 || arg1->is_unsigned))
1701 : {
1702 0 : bool is_greater_than = check_const_value_is_greater_than (arg1,
1703 : arg2);
1704 0 : bool is_equal = check_const_value_equality (arg1, arg2);
1705 0 : last_cond_status = (is_greater_than | is_equal)
1706 0 : ? condition_status::CS_TRUE
1707 : : condition_status::CS_FALSE;
1708 0 : return;
1709 : }
1710 :
1711 2128 : last_cond_status = condition_status::CS_SYM;
1712 2128 : if (is_bit_vector (arg2) && make_number (arg2) == 0 && !arg1->is_unsigned)
1713 : {
1714 2128 : if (is_a<bit *> (arg1->last ()))
1715 : {
1716 0 : if (as_a<bit *> (arg1->last ())->get_val () == 1)
1717 0 : last_cond_status = condition_status::CS_FALSE;
1718 : else
1719 0 : last_cond_status = condition_status::CS_TRUE;
1720 : }
1721 : else
1722 4256 : conditions.add (new bit_condition (arg1->last ()->copy (), new bit (0),
1723 4256 : EQ_EXPR));
1724 :
1725 : return;
1726 : }
1727 :
1728 0 : bit_expression *eq_cond = construct_equal_cond (arg1, arg2);
1729 0 : if (!eq_cond)
1730 : return;
1731 :
1732 0 : bit_expression *gt_cond = construct_great_than_cond (arg1, arg2);
1733 0 : if (gt_cond)
1734 : /* Otherwise its status is already set. */
1735 0 : conditions.add (new bit_or_expression (eq_cond, gt_cond));
1736 : }
1737 :
1738 :
1739 : /* Adds LESS OR EQUAL condition of given variables to state. */
1740 :
1741 : bool
1742 0 : state::add_less_or_equal_cond (tree arg1, tree arg2)
1743 : {
1744 0 : return add_binary_cond (arg1, arg2, &state::add_less_or_equal_cond);
1745 : }
1746 :
1747 :
1748 : /* Adds less or equal condition for two values. */
1749 :
1750 : void
1751 0 : state::add_less_or_equal_cond (value *arg1, value *arg2)
1752 : {
1753 0 : if (is_bit_vector (arg1) && is_bit_vector (arg2))
1754 : {
1755 0 : bool is_less_than = check_const_value_is_less_than (arg1, arg2);
1756 0 : bool is_equal = check_const_value_equality (arg1, arg2);
1757 0 : last_cond_status = (is_less_than | is_equal)
1758 0 : ? condition_status::CS_TRUE
1759 : : condition_status::CS_FALSE;
1760 0 : return;
1761 : }
1762 :
1763 0 : last_cond_status = condition_status::CS_SYM;
1764 0 : bit_expression *eq_cond = construct_equal_cond (arg1, arg2);
1765 0 : if (!eq_cond)
1766 : return;
1767 :
1768 0 : bit_expression *lt_cond = construct_less_than_cond (arg1, arg2);
1769 0 : if (lt_cond)
1770 : /* Otherwise its status is already set. */
1771 0 : conditions.add (new bit_or_expression (eq_cond, lt_cond));
1772 : }
1773 :
1774 :
1775 : /* Adds a bool condition to state. */
1776 :
1777 : bool
1778 0 : state::add_bool_cond (tree arg)
1779 : {
1780 0 : if (!is_declared (arg))
1781 : {
1782 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1783 0 : fprintf (dump_file, "Sym-Exec: Argument must be declared "
1784 : "for bool condition.\n");
1785 :
1786 : return false;
1787 : }
1788 :
1789 0 : value *arg_bits = var_states.get (arg);
1790 0 : for (size_t i = 0; i < arg_bits->length (); i++)
1791 0 : if (is_a<bit *> ((*arg_bits)[i])
1792 0 : && as_a<bit *> ((*arg_bits)[i])->get_val () != 0)
1793 : {
1794 0 : last_cond_status = condition_status::CS_TRUE;
1795 0 : print_conditions ();
1796 0 : return true;
1797 : }
1798 :
1799 0 : if (is_bit_vector (arg_bits))
1800 : {
1801 0 : last_cond_status = condition_status::CS_FALSE;
1802 0 : print_conditions ();
1803 0 : return true;
1804 : }
1805 :
1806 0 : bit_expression *prev = nullptr;
1807 0 : for (size_t i = 0; i < arg_bits->length (); i++)
1808 : {
1809 0 : if (is_a<bit *> ((*arg_bits)[i]))
1810 0 : continue;
1811 :
1812 0 : bit_condition *not_eq_cond
1813 0 : = new bit_condition ((*arg_bits)[i], new bit (0), NE_EXPR);
1814 0 : if (prev)
1815 0 : prev = new bit_or_expression (not_eq_cond, prev);
1816 : else
1817 0 : prev = not_eq_cond;
1818 : }
1819 :
1820 0 : last_cond_status = condition_status::CS_SYM;
1821 0 : conditions.add (prev);
1822 0 : print_conditions ();
1823 0 : return true;
1824 : }
1825 :
1826 :
1827 : /* Does preprocessing and postprocessing for condition adding.
1828 : Handles value creation for constants and their removement in the end. */
1829 :
1830 : bool
1831 24172 : state::add_binary_cond (tree arg1, tree arg2, binary_cond_func cond_func)
1832 : {
1833 24172 : bool arg1_is_declared = is_declared (arg1);
1834 24172 : bool arg2_is_declared = is_declared (arg2);
1835 :
1836 24172 : if (!arg1_is_declared && !arg2_is_declared)
1837 : {
1838 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1839 0 : fprintf (dump_file, "Sym-Exec: At least one of arguments must be"
1840 : " declared for adding the condition.\n");
1841 :
1842 : return false;
1843 : }
1844 :
1845 24172 : if (arg1_is_declared)
1846 24172 : declare_if_needed (arg2, var_states.get (arg1)->length ());
1847 :
1848 24172 : if (arg2_is_declared)
1849 0 : declare_if_needed (arg1, var_states.get (arg2)->length ());
1850 :
1851 24172 : value *arg1_val = var_states.get (arg1);
1852 24172 : value arg1_const_val (MAX_VALUE_SIZE, false);
1853 :
1854 24172 : if (arg1_val == NULL && TREE_CODE (arg1) == INTEGER_CST)
1855 : {
1856 0 : arg1_const_val = create_val_for_const (arg1,
1857 0 : var_states.get (arg2)->length ());
1858 0 : arg1_val = &arg1_const_val;
1859 : }
1860 :
1861 24172 : value *arg2_val = var_states.get (arg2);
1862 24172 : value arg2_const_val (MAX_VALUE_SIZE, false);
1863 24172 : if (arg2_val == NULL && TREE_CODE (arg2) == INTEGER_CST)
1864 : {
1865 48344 : arg2_const_val = create_val_for_const (arg2,
1866 48344 : var_states.get (arg1)->length ());
1867 24172 : arg2_val = &arg2_const_val;
1868 : }
1869 :
1870 24172 : (this->*cond_func) (arg1_val, arg2_val);
1871 24172 : print_conditions ();
1872 24172 : return true;
1873 24172 : }
1874 :
1875 :
1876 : /* Constructs expression trees of equal condition for given values. */
1877 :
1878 : bit_expression *
1879 0 : state::construct_equal_cond (value *arg1, value *arg2)
1880 : {
1881 : /* If both arguments are constants then we can evaluate it. */
1882 0 : if (is_bit_vector (arg1) && is_bit_vector (arg2))
1883 : {
1884 0 : bool result = check_const_value_equality (arg1, arg2);
1885 0 : last_cond_status = result ? condition_status::CS_TRUE
1886 : : condition_status::CS_FALSE;
1887 0 : return nullptr;
1888 : }
1889 :
1890 : /* When some bits are constants, and they differ by value,
1891 : then we can evaluate it to be false. */
1892 0 : for (size_t i = 0; i < arg1->length (); i++)
1893 : {
1894 0 : if (is_a<bit *> ((*arg1)[i]) && is_a<bit *> ((*arg2)[i])
1895 0 : && as_a<bit *> ((*arg1)[i])->get_val ()
1896 0 : != as_a<bit *> ((*arg2)[i])->get_val ())
1897 : {
1898 0 : last_cond_status = condition_status::CS_FALSE;
1899 0 : return nullptr;
1900 : }
1901 : }
1902 :
1903 : bit_expression *prev = nullptr;
1904 0 : for (size_t i = 0; i < arg1->length (); i++)
1905 : {
1906 0 : bit_condition *eq_expr = new bit_condition ((*arg1)[i]->copy (),
1907 0 : (*arg2)[i]->copy (), EQ_EXPR);
1908 0 : if (prev)
1909 0 : prev = new bit_and_expression (eq_expr, prev);
1910 : else
1911 : prev = eq_expr;
1912 : }
1913 :
1914 : return prev;
1915 : }
1916 :
1917 :
1918 : /* Constructor for value. The first argument is the size of the bit-vector
1919 : and the second argument is the sign of the number. */
1920 :
1921 552173 : value::value (unsigned size, bool is_unsigned) : is_unsigned (is_unsigned)
1922 : {
1923 552173 : number.create (size);
1924 552173 : }
1925 :
1926 :
1927 : /* Copy constructor for value. */
1928 :
1929 438342 : value::value (const value &other) : is_unsigned (other.is_unsigned)
1930 : {
1931 438342 : number.create (other.length ());
1932 18919368 : for (size_t i = 0; i < other.length (); ++i)
1933 : {
1934 18042684 : value_bit *temp = other[i] ? other[i]->copy () : other[i];
1935 18042684 : number.quick_push (temp);
1936 : }
1937 438342 : }
1938 :
1939 :
1940 : /* Returns pushed bits count. */
1941 :
1942 : unsigned
1943 33719211 : value::length () const
1944 : {
1945 33719211 : return number.length ();
1946 : }
1947 :
1948 :
1949 : /* Wrapper of vec<..>::operator[] for the bit-vector. */
1950 :
1951 : value_bit *&
1952 25366108 : value::operator[] (unsigned i)
1953 : {
1954 25366108 : return number[i];
1955 : }
1956 :
1957 :
1958 : /* Assignment operator. If the specified value's size is smaller,
1959 : then 0 constant bit will be assigned to the remaining upper bits. */
1960 :
1961 : value &
1962 73183 : value::operator= (const value &other)
1963 : {
1964 146366 : unsigned smallest = number.allocated () < other.length ()
1965 73183 : ? number.allocated () : other.length ();
1966 :
1967 2856151 : for (size_t i = 0; i < smallest; i++)
1968 2782968 : if (i < number.length ())
1969 : {
1970 0 : delete number[i];
1971 0 : number[i] = other[i]->copy ();
1972 : }
1973 : else
1974 2782968 : number.quick_push (other[i]->copy ());
1975 :
1976 696199 : for (size_t i = smallest; i < number.allocated (); i++)
1977 623016 : if (i < number.length ())
1978 : {
1979 0 : delete number[i];
1980 0 : number[i] = other.is_unsigned ? new bit (0)
1981 0 : : other[other.length () - 1]->copy ();
1982 : }
1983 : else
1984 1246032 : number.quick_push (other.is_unsigned
1985 623016 : ? new bit (0) : other[other.length () - 1]->copy ());
1986 :
1987 73183 : return (*this);
1988 : }
1989 :
1990 :
1991 : /* Wrapper of vec<..>::operator[] const for the bit-vector. */
1992 :
1993 : value_bit *
1994 46852554 : value::operator[] (unsigned i) const
1995 : {
1996 46852554 : return number[i];
1997 : }
1998 :
1999 :
2000 : /* Wrapper of vec<..>.exists for the bit-vector. */
2001 :
2002 : bool
2003 77542 : value::exists () const
2004 : {
2005 77542 : return number.exists ();
2006 : }
2007 :
2008 :
2009 : /* Returns the size in bits. */
2010 :
2011 : unsigned
2012 889623 : value::allocated () const
2013 : {
2014 889623 : return number.allocated ();
2015 : }
2016 :
2017 :
2018 : /* Returns a reference the last bit. */
2019 :
2020 : value_bit *&
2021 9572 : value::last ()
2022 : {
2023 9572 : return number.last ();
2024 : }
2025 :
2026 :
2027 : /* Make a copy of given bits. */
2028 :
2029 : vec<value_bit *> *
2030 0 : state::make_copy (vec<value_bit *> *bits)
2031 : {
2032 0 : vec < value_bit * > *copied_bits = new vec<value_bit *> ();
2033 0 : copied_bits->create (bits->length ());
2034 0 : for (size_t i = 0; i < bits->length (); i++)
2035 0 : copied_bits->quick_push ((*bits)[i]->copy ());
2036 :
2037 0 : return copied_bits;
2038 : }
2039 :
2040 :
2041 : /* Returns status of last added condition. */
2042 :
2043 : condition_status
2044 49878 : state::get_last_cond_status ()
2045 : {
2046 49878 : return last_cond_status;
2047 : }
2048 :
2049 :
2050 : /* Prints the given value. */
2051 :
2052 : void
2053 92490 : state::print_value (value *var)
2054 : {
2055 92490 : if (!dump_file || !(dump_flags & TDF_DETAILS))
2056 : return;
2057 :
2058 68053 : fprintf (dump_file, "{");
2059 2741701 : for (int i = var->length () - 1; i >= 0; i--)
2060 : {
2061 2673648 : (*var)[i]->print ();
2062 :
2063 2673648 : if (i)
2064 2605595 : fprintf (dump_file, ", ");
2065 : }
2066 68053 : fprintf (dump_file, "}\n");
2067 : }
2068 :
2069 :
2070 : /* Create LFSR value for the reversed CRC. */
2071 :
2072 : void
2073 195 : state::create_reversed_lfsr (value &lfsr, const value &crc,
2074 : const value &polynomial)
2075 : {
2076 195 : size_t size = polynomial.length ();
2077 :
2078 : /* Determine values of all bits, except MSB. */
2079 6936 : for (size_t i = 0; i < size - 1; i++)
2080 : {
2081 6741 : if (as_a<bit *> (polynomial[i])->get_val ())
2082 2530 : lfsr.push (state::xor_two_bits (crc[i + 1], crc[0]));
2083 : else
2084 4211 : lfsr.push (crc[i + 1]->copy ());
2085 : }
2086 :
2087 : /* Determine value of MSB. */
2088 195 : if (as_a<bit *> (polynomial[size - 1])->get_val ())
2089 159 : lfsr.push (crc[0]->copy ());
2090 : else
2091 36 : lfsr.push (new bit (0));
2092 195 : }
2093 :
2094 :
2095 : /* Create LFSR value for the forward CRC. */
2096 :
2097 : void
2098 165 : state::create_forward_lfsr (value &lfsr, const value &crc,
2099 : const value &polynomial)
2100 : {
2101 165 : size_t size = polynomial.length ();
2102 : /* Determine value of LSB. */
2103 165 : if (as_a<bit *> (polynomial[0])->get_val ())
2104 144 : lfsr.push (crc[size - 1]->copy ());
2105 : else
2106 21 : lfsr.push (new bit (0));
2107 :
2108 : /* Determine values of remaining bits. */
2109 4312 : for (size_t i = 1; i < size; i++)
2110 : {
2111 4147 : if (as_a<bit *> (polynomial[i])->get_val ())
2112 1599 : lfsr.push (state::xor_two_bits (crc[i - 1], crc[size - 1]));
2113 : else
2114 2548 : lfsr.push (crc[i - 1]->copy ());
2115 : }
2116 165 : }
2117 :
2118 :
2119 : /* Get the last 1 bit index. */
2120 :
2121 : size_t
2122 385 : last_set_bit (const value &polynomial)
2123 : {
2124 1625 : for (size_t i = 0; i < polynomial.length (); ++i)
2125 : {
2126 1607 : if (as_a<bit *> (polynomial[polynomial.length () - i - 1])->get_val ())
2127 367 : return polynomial.length () - i - 1;
2128 : }
2129 : return 0;
2130 : }
2131 :
2132 :
2133 : /* Create LFSR value. */
2134 :
2135 : value *
2136 385 : state::create_lfsr (tree crc, value *polynomial, bool is_bit_forward)
2137 : {
2138 : /* Check size compatibility․ */
2139 385 : unsigned HOST_WIDE_INT polynomial_length = polynomial->length ();
2140 385 : unsigned HOST_WIDE_INT crc_size = TYPE_PRECISION (TREE_TYPE (crc));
2141 385 : if (crc_size < polynomial_length)
2142 : {
2143 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2144 0 : fprintf (dump_file, "LFSR state creation: "
2145 : "Polynomial doesn't fit into the crc.\n");
2146 :
2147 : return nullptr;
2148 : }
2149 :
2150 : /* Get the minimal byte size to keep the polynomial.
2151 : Ie, if the last 1 bit of the polynomial is at 6 index, size will be 8. */
2152 385 : size_t required_polynomial_size = ((last_set_bit (*polynomial)/8) + 1) * 8;
2153 :
2154 : /* Polynomial's length actually equals to the CRC variable's size.
2155 : Now we detect only those CRC calculation algorithms, where leading 1 of the
2156 : polynomial isn't kept. */
2157 385 : if (required_polynomial_size == 0
2158 385 : || required_polynomial_size != polynomial_length)
2159 : {
2160 25 : if (dump_file && (dump_flags & TDF_DETAILS))
2161 20 : fprintf (dump_file, "Polynomial's all bits are zeros "
2162 : "or the size of the polynomial is uncertain.\n");
2163 : return nullptr;
2164 : }
2165 :
2166 : /* Create vector of symbolic bits for crc. */
2167 360 : value crc_value (polynomial_length, TYPE_UNSIGNED (TREE_TYPE (crc)));
2168 :
2169 11968 : for (unsigned HOST_WIDE_INT i = 0; i < polynomial_length; i++)
2170 11248 : crc_value.push (new symbolic_bit (i, crc));
2171 :
2172 : /* create LFSR vector. */
2173 360 : value *lfsr = new value (polynomial_length, TYPE_UNSIGNED (TREE_TYPE (crc)));
2174 :
2175 : /* Calculate values for LFSR. */
2176 360 : if (is_bit_forward)
2177 165 : create_forward_lfsr (*lfsr, crc_value, *polynomial);
2178 : else
2179 195 : create_reversed_lfsr (*lfsr, crc_value, *polynomial);
2180 :
2181 360 : return lfsr;
2182 360 : }
2183 :
2184 :
2185 : /* Prints added conditions. */
2186 :
2187 : void
2188 24172 : state::print_conditions ()
2189 : {
2190 24172 : if (!dump_file || !(dump_flags & TDF_DETAILS))
2191 7316 : return;
2192 :
2193 16856 : fprintf (dump_file, "Conditions {");
2194 16856 : auto iter = conditions.begin ();
2195 16856 : while (true)
2196 : {
2197 16856 : if (iter != conditions.end ())
2198 : {
2199 16344 : (*iter)->print ();
2200 16344 : ++iter;
2201 : }
2202 :
2203 16856 : if (iter != conditions.end ())
2204 0 : fprintf (dump_file, ", ");
2205 : else
2206 : break;
2207 : }
2208 16856 : fprintf (dump_file, "}\n");
2209 : }
2210 :
2211 :
2212 : /* Pushes the given bit to the end of the bit vector. */
2213 :
2214 : value_bit **
2215 15323924 : value::push (value_bit *elem)
2216 : {
2217 15323924 : return number.quick_push (elem);
2218 : }
2219 :
2220 :
2221 : /* Destructor for value. */
2222 :
2223 990515 : value::~value ()
2224 : {
2225 990515 : free_bits ();
2226 990515 : number.release ();
2227 990515 : }
2228 :
2229 :
2230 : /* Removes given sequence of bits. */
2231 :
2232 : void
2233 990515 : value::free_bits ()
2234 : {
2235 990515 : if (!number.exists ())
2236 : return;
2237 :
2238 37763107 : for (size_t i = 0; i < number.length (); i++)
2239 : {
2240 36772592 : delete number[i];
2241 36772592 : number[i] = nullptr;
2242 : }
2243 : }
2244 :
2245 :
2246 : /* For the given value_bit, iterates over its expression tree, complements
2247 : those bit which came from the given origin. */
2248 :
2249 : value_bit *
2250 0 : state::complement_bits_with_origin (value_bit *root, tree origin)
2251 : {
2252 : /* Be careful. This function doesn't make a full copy of the bit. */
2253 0 : if (!is_a<bit_expression *> (root))
2254 : {
2255 0 : if (is_a<symbolic_bit *> (root)
2256 0 : && as_a<symbolic_bit *> (root)->get_origin () == origin)
2257 0 : root = new bit_complement_expression (root);
2258 :
2259 : return root;
2260 : }
2261 :
2262 0 : bit_expression *expr_root = as_a<bit_expression *> (root);
2263 0 : hash_set <value_bit *> nodes_to_consider;
2264 0 : nodes_to_consider.add (expr_root);
2265 0 : hash_map <value_bit *, value_bit *> node_to_parent;
2266 0 : node_to_parent.put (expr_root, nullptr);
2267 :
2268 : /* Traversing expression tree. */
2269 0 : while (!nodes_to_consider.is_empty ())
2270 : {
2271 0 : value_bit *cur_element = *nodes_to_consider.begin ();
2272 0 : nodes_to_consider.remove (cur_element);
2273 :
2274 0 : if (is_a<symbolic_bit *> (cur_element))
2275 : {
2276 0 : if (as_a<symbolic_bit *> (cur_element)->get_origin () != origin)
2277 0 : continue;
2278 :
2279 0 : bit_expression *parent
2280 0 : = as_a<bit_expression *> (*node_to_parent.get (cur_element));
2281 0 : if (is_a<bit_complement_expression *> (parent))
2282 : {
2283 0 : value_bit *parent_of_parent = *node_to_parent.get (parent);
2284 0 : if (parent_of_parent)
2285 : {
2286 0 : bit_expression *parent_of_parent_expr
2287 0 : = as_a<bit_expression *> (parent_of_parent);
2288 0 : parent->set_right (nullptr);
2289 0 : delete parent;
2290 0 : parent_of_parent_expr->get_left () == parent
2291 0 : ? parent_of_parent_expr->set_left (cur_element)
2292 0 : : parent_of_parent_expr->set_right (cur_element);
2293 : }
2294 : else
2295 : {
2296 : /* Parent is our root. */
2297 0 : as_a<bit_expression *> (root)->set_right (nullptr);
2298 0 : delete root;
2299 : root = cur_element;
2300 : }
2301 : }
2302 : else
2303 : {
2304 0 : value_bit* new_bit = new bit_complement_expression (cur_element);
2305 0 : parent->get_left () == cur_element ? parent->set_left (new_bit)
2306 0 : : parent->set_right (new_bit);
2307 : }
2308 0 : continue;
2309 0 : }
2310 :
2311 0 : bit_expression* cur_elem_expr = as_a<bit_expression *> (cur_element);
2312 0 : value_bit *left = cur_elem_expr->get_left ();
2313 0 : value_bit *right = cur_elem_expr->get_right ();
2314 0 : if (left != nullptr && !is_a<bit *> (left))
2315 : {
2316 0 : nodes_to_consider.add (left);
2317 0 : node_to_parent.put (left, cur_element);
2318 : }
2319 :
2320 0 : if (right != nullptr && !is_a<bit *> (right))
2321 : {
2322 0 : nodes_to_consider.add (right);
2323 0 : node_to_parent.put (right, cur_element);
2324 : }
2325 : }
2326 :
2327 0 : return root;
2328 0 : }
2329 :
2330 :
2331 : /* Iterates over every bit of the given value and complements their
2332 : expression trees' those bits, that came from the given origin. */
2333 :
2334 : void
2335 0 : state::complement_val_bits_with_origin (value *val, tree origin)
2336 : {
2337 0 : for (size_t i = 0; i < val->length (); i++)
2338 : {
2339 0 : (*val)[i] = complement_bits_with_origin ((*val)[i], origin);
2340 : }
2341 0 : }
2342 :
2343 :
2344 : /* Complements all bits of all values that came from the given origin. */
2345 :
2346 : void
2347 0 : state::complement_all_vars_bits_with_origin (tree origin)
2348 : {
2349 0 : for (auto iter = var_states.begin (); iter != var_states.end (); ++iter)
2350 : {
2351 0 : complement_val_bits_with_origin (&(*iter).second, origin);
2352 : }
2353 0 : }
2354 :
2355 :
2356 : /* Complements all bits with the given origin of all added conditions. */
2357 :
2358 : void
2359 0 : state::complement_conditions_with_origin (tree origin)
2360 : {
2361 0 : hash_set<bit_expression *> updated_conditions;
2362 0 : for (auto iter = conditions.begin (); iter != conditions.end (); ++iter)
2363 0 : updated_conditions.add (as_a<bit_expression *> (
2364 0 : complement_bits_with_origin (*iter, origin)));
2365 :
2366 0 : conditions.empty ();
2367 0 : for (auto iter = updated_conditions.begin ();
2368 0 : iter != updated_conditions.end (); ++iter)
2369 0 : conditions.add (*iter);
2370 0 : }
2371 :
2372 :
2373 : /* Complements all bits with the given origin of all values
2374 : and added conditions. */
2375 :
2376 : void
2377 0 : state::complement_state_with_origin (tree origin)
2378 : {
2379 0 : complement_all_vars_bits_with_origin (origin);
2380 0 : complement_conditions_with_origin (origin);
2381 0 : }
2382 :
2383 :
2384 : /* Performs the specified operation on passed variables. */
2385 :
2386 : bool
2387 83441 : state::do_operation (tree_code op_code, tree arg1, tree arg2, tree dest)
2388 : {
2389 83441 : switch (op_code)
2390 : {
2391 0 : case BIT_NOT_EXPR:
2392 0 : return do_complement (arg1, dest);
2393 30168 : case NOP_EXPR:
2394 30168 : case SSA_NAME:
2395 30168 : case VAR_DECL:
2396 30168 : case INTEGER_CST:
2397 30168 : return do_assign (arg1, dest);
2398 6490 : case LSHIFT_EXPR:
2399 6490 : return do_binary_operation (arg1, arg2, dest, &state::do_shift_left);
2400 8023 : case RSHIFT_EXPR:
2401 8023 : return do_binary_operation (arg1, arg2, dest, &state::do_shift_right);
2402 4066 : case BIT_AND_EXPR:
2403 4066 : return do_binary_operation (arg1, arg2, dest, &state::do_and);
2404 4 : case BIT_IOR_EXPR:
2405 4 : return do_binary_operation (arg1, arg2, dest, &state::do_or);
2406 10467 : case BIT_XOR_EXPR:
2407 10467 : return do_binary_operation (arg1, arg2, dest, &state::do_xor);
2408 12129 : case PLUS_EXPR:
2409 12129 : return do_binary_operation (arg1, arg2, dest, &state::do_add);
2410 12088 : case MINUS_EXPR:
2411 12088 : return do_binary_operation (arg1, arg2, dest, &state::do_sub);
2412 6 : case MULT_EXPR:
2413 6 : return do_binary_operation (arg1, arg2, dest, &state::do_mul);
2414 0 : default:
2415 0 : {
2416 0 : if (dump_file)
2417 0 : fprintf (dump_file,
2418 : "Warning, encountered unsupported operation "
2419 : "with %s code while executing assign statement!\n",
2420 : get_tree_code_name (op_code));
2421 : return false;
2422 : }
2423 : }
2424 : }
|