Debian Codegraph @

gcc/sym-exec/sym-exec-expression.cc

raw
1 /* Every class defined here represents a single bit value of a variable.
2 Every variable will be represented as a vector of these classes which later
3 will be used for bit-level symbolic execution.
4 Copyright (C) 2022-2025 Free Software Foundation, Inc.
5 Contributed by Matevos Mehrabyan <[email protected]>
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 #include "sym-exec-expr-is-a-helper.h"
24
25 /* Returns type of the bit. */
26
27 value_type
28 value_bit::get_type () const
29 {
30 return m_type;
31 }
32
33
34 /* Constructor that sets the bit's initial position and its origin. */
35
36 symbolic_bit::symbolic_bit (size_t i, tree orig)
37 : value_bit (i), m_origin (orig)
38 {
39 m_type = SYMBOLIC_BIT;
40 }
41
42
43 /* Constructor that sets m_val to the specified value. */
44
45 bit::bit (unsigned char i) : m_val (i)
46 {
47 m_type = BIT;
48 }
49
50
51 /* Returns left operand of the expression. */
52
53 value_bit *
54 bit_expression::get_left ()
55 {
56 return m_left;
57 }
58
59
60 /* Returns right operand of the expression. */
61
62 value_bit *
63 bit_expression::get_right ()
64 {
65 return m_right;
66 }
67
68
69 /* Sets left operand of the expression. */
70
71 void
72 bit_expression::set_left (value_bit *expr)
73 {
74 m_left = expr;
75 }
76
77
78 /* Sets right operand of the expression. */
79
80 void
81 bit_expression::set_right (value_bit *expr)
82 {
83 m_right = expr;
84 }
85
86
87 /* Returns the bit's initial index in bit-vector. */
88
89 size_t
90 value_bit::get_index () const
91 {
92 return m_index;
93 }
94
95
96 /* Returns the value of the bit. */
97
98 unsigned char
99 bit::get_val () const
100 {
101 return m_val;
102 }
103
104
105 /* Sets the value of the bit. */
106
107 void
108 bit::set_val (unsigned char new_val)
109 {
110 m_val = new_val;
111 }
112
113
114 /* Constructor that sets the left and right side bits
115 of the bit_complement_expression sign. */
116
117 bit_complement_expression::bit_complement_expression (value_bit *right)
118 {
119 /* As complement has only one argument, we use only the m_right. */
120 this->m_left = nullptr;
121 this->m_right = right;
122 m_type = BIT_COMPLEMENT_EXPRESSION;
123 }
124
125
126 /* Copy constructor for bit_complement_expression. */
127
128 bit_complement_expression::bit_complement_expression (
129 const bit_complement_expression &expr)
130 {
131 bit_expression::copy (&expr);
132 }
133
134
135 /* Destructor for bit_expression. */
136
137 bit_expression::~bit_expression ()
138 {
139 delete m_left;
140 m_left = nullptr;
141 delete m_right;
142 m_right = nullptr;
143 }
144
145
146 /* Returns a copy of the bit. */
147
148 value_bit *
149 symbolic_bit::copy () const
150 {
151 return new symbolic_bit (*this);
152 }
153
154
155 /* Return a copy of the bit. */
156
157 value_bit *
158 bit::copy () const
159 {
160 return new bit (*this);
161 }
162
163
164 /* Copies the given expression to it by copying the left and right operands. */
165
166 void
167 bit_expression::copy (const bit_expression *expr)
168 {
169 if (expr->m_left)
170 m_left = expr->m_left->copy ();
171
172 if (expr->m_right)
173 m_right = expr->m_right->copy ();
174
175 m_type = expr->m_type;
176 }
177
178
179 /* Returns a copy of the expression. */
180
181 value_bit *
182 bit_xor_expression::copy () const
183 {
184 return new bit_xor_expression (*this);
185 }
186
187
188 /* Returns a copy of the expression. */
189
190 value_bit *
191 bit_and_expression::copy () const
192 {
193 return new bit_and_expression (*this);
194 }
195
196
197 /* Returns a copy of the expression. */
198
199 value_bit *
200 bit_or_expression::copy () const
201 {
202 return new bit_or_expression (*this);
203 }
204
205
206 /* Returns a copy of the expression. */
207
208 value_bit *
209 shift_right_expression::copy () const
210 {
211 return new shift_right_expression (*this);
212 }
213
214
215 /* Returns a copy of the expression. */
216
217 value_bit *
218 shift_left_expression::copy () const
219 {
220 return new shift_left_expression (*this);
221 }
222
223
224 /* Returns a copy of the expression. */
225
226 value_bit *
227 add_expression::copy () const
228 {
229 return new add_expression (*this);
230 }
231
232
233 /* Returns a copy of the expression. */
234
235 value_bit *
236 sub_expression::copy () const
237 {
238 return new sub_expression (*this);
239 }
240
241
242 /* Returns a copy of the expression. */
243
244 value_bit *
245 bit_complement_expression::copy () const
246 {
247 return new bit_complement_expression (*this);
248 }
249
250
251 /* Constructor that sets the left and right side bits
252 of the bit_xor_expression sign. */
253
254 bit_xor_expression::bit_xor_expression (value_bit *left, value_bit *right)
255 {
256 this->m_left = left;
257 this->m_right = right;
258 m_type = BIT_XOR_EXPRESSION;
259 }
260
261
262 /* Copy constructor for bit_xor_expression. */
263
264 bit_xor_expression::bit_xor_expression (const bit_xor_expression &expr)
265 {
266 bit_expression::copy (&expr);
267 }
268
269
270 /* Constructor that sets the left and right side bits
271 of the bit_and_expression sign. */
272
273 bit_and_expression::bit_and_expression (value_bit *left, value_bit *right)
274 {
275 this->m_left = left;
276 this->m_right = right;
277 m_type = BIT_AND_EXPRESSION;
278 }
279
280
281 /* Copy constructor for bit_and_expression. */
282
283 bit_and_expression::bit_and_expression (const bit_and_expression &expr)
284 {
285 bit_expression::copy (&expr);
286 }
287
288
289 /* Constructor that sets the left and right side bits
290 of the bit_or_expression sign. */
291
292 bit_or_expression::bit_or_expression (value_bit *left, value_bit *right)
293 {
294 this->m_left = left;
295 this->m_right = right;
296 m_type = BIT_OR_EXPRESSION;
297 }
298
299
300 /* Copy constructor for bit_or_expression. */
301
302 bit_or_expression::bit_or_expression (const bit_or_expression &expr)
303 {
304 bit_expression::copy (&expr);
305 }
306
307
308 /* Constructor that sets the left and right side bits
309 of the shift_right_expression sign. */
310
311 shift_right_expression::shift_right_expression (value_bit *left,
312 value_bit *right)
313 {
314 this->m_left = left;
315 this->m_right = right;
316 m_type = SHIFT_RIGHT_EXPRESSION;
317 }
318
319
320 /* Copy constructor for shift_right_expression. */
321
322 shift_right_expression::shift_right_expression (
323 const shift_right_expression &expr)
324 {
325 bit_expression::copy (&expr);
326 }
327
328
329 /* Constructor that sets the left and right side bits
330 of the shift_left_expression sign. */
331
332 shift_left_expression::shift_left_expression (value_bit *left, value_bit *right)
333 {
334 this->m_left = left;
335 this->m_right = right;
336 m_type = SHIFT_LEFT_EXPRESSION;
337 }
338
339
340 /* Copy constructor for shift_left_expression. */
341
342 shift_left_expression::shift_left_expression (const shift_left_expression &expr)
343 {
344 bit_expression::copy (&expr);
345 }
346
347
348 /* Constructor that sets the left and right side bits
349 of the add_expression sign. */
350
351 add_expression::add_expression (value_bit *left, value_bit *right)
352 {
353 this->m_left = left;
354 this->m_right = right;
355 m_type = ADD_EXPRESSION;
356 }
357
358
359 /* Copy constructor for add_expression. */
360
361 add_expression::add_expression (const add_expression &expr)
362 {
363 bit_expression::copy (&expr);
364 }
365
366
367 /* Constructor that sets the left and right side bits
368 of the sub_expression sign. */
369
370 sub_expression::sub_expression (value_bit *left, value_bit *right)
371 {
372 this->m_left = left;
373 this->m_right = right;
374 m_type = SUB_EXPRESSION;
375 }
376
377
378 /* Copy constructor for sub_expression. */
379
380 sub_expression::sub_expression (const sub_expression &expr)
381 {
382 bit_expression::copy (&expr);
383 }
384
385
386 /* Returns the origin of the bit, to whom it belongs. */
387
388 tree
389 symbolic_bit::get_origin ()
390 {
391 return m_origin;
392 }
393
394
395 /* Prints the bit. */
396
397 void
398 symbolic_bit::print ()
399 {
400 if (dump_file)
401 {
402 print_generic_expr (dump_file, m_origin, dump_flags);
403 fprintf (dump_file, "[%zu]", m_index);
404 }
405 }
406
407
408 /* Prints the bit. */
409
410 void
411 bit::print ()
412 {
413 if (dump_file)
414 fprintf (dump_file, "%u", m_val);
415 }
416
417
418 /* Depending on the expression, prints its sign. */
419
420 void
421 bit_expression::print_expr_sign ()
422 {
423 switch (m_type)
424 {
425 case BIT_XOR_EXPRESSION:
426 fprintf (dump_file, " ^ ");
427 break;
428 case BIT_AND_EXPRESSION:
429 fprintf (dump_file, " & ");
430 break;
431 case BIT_OR_EXPRESSION:
432 fprintf (dump_file, " | ");
433 break;
434 case SHIFT_RIGHT_EXPRESSION:
435 fprintf (dump_file, " >> ");
436 break;
437 case SHIFT_LEFT_EXPRESSION:
438 fprintf (dump_file, " << ");
439 break;
440 case ADD_EXPRESSION:
441 fprintf (dump_file, " + ");
442 break;
443 case SUB_EXPRESSION:
444 fprintf (dump_file, " - ");
445 break;
446 default:
447 fprintf (dump_file, " ?? ");
448 }
449 }
450
451
452 /* Prints the expression. */
453
454 void
455 bit_expression::print ()
456 {
457 if (dump_file)
458 {
459 fprintf (dump_file, "(");
460 if (m_left)
461 m_left->print ();
462 else
463 fprintf (dump_file, "null");
464
465 print_expr_sign ();
466
467 if (m_right)
468 m_right->print ();
469 else
470 fprintf (dump_file, "null");
471
472 fprintf (dump_file, ")");
473 }
474 }
475
476
477 /* Prints the expression. */
478
479 void
480 bit_complement_expression::print ()
481 {
482 if (dump_file)
483 {
484 fprintf (dump_file, "!");
485 if (m_right)
486 m_right->print ();
487 else
488 fprintf (dump_file, "null");
489 }
490 }