aboutsummaryrefslogtreecommitdiff
path: root/gcc/sym-exec/sym-exec-expression.cc
blob: fd20a9b7a2949476fc1a3f946834999dc51e0c9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* Every class defined here represents a single bit value of a variable.
   Every variable will be represented as a vector of these classes which later
   will be used for bit-level symbolic execution.
   Copyright (C) 2022-2025 Free Software Foundation, Inc.
   Contributed by Matevos Mehrabyan <matevosmehrabyan@gmail.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "sym-exec-expr-is-a-helper.h"

/* Returns type of the bit.  */

value_type
value_bit::get_type () const
{
  return m_type;
}


/* Constructor that sets the bit's initial position and its origin.  */

symbolic_bit::symbolic_bit (size_t i, tree orig)
    : value_bit (i), m_origin (orig)
{
  m_type = SYMBOLIC_BIT;
}


/* Constructor that sets m_val to the specified value.  */

bit::bit (unsigned char i) : m_val (i)
{
  m_type = BIT;
}


/* Returns left operand of the expression.  */

value_bit *
bit_expression::get_left ()
{
  return m_left;
}


/* Returns right operand of the expression.  */

value_bit *
bit_expression::get_right ()
{
  return m_right;
}


/* Sets left operand of the expression.  */

void
bit_expression::set_left (value_bit *expr)
{
  m_left = expr;
}


/* Sets right operand of the expression.  */

void
bit_expression::set_right (value_bit *expr)
{
  m_right = expr;
}


/* Returns the bit's initial index in bit-vector.  */

size_t
value_bit::get_index () const
{
  return m_index;
}


/* Returns the value of the bit.  */

unsigned char
bit::get_val () const
{
  return m_val;
}


/* Sets the value of the bit.  */

void
bit::set_val (unsigned char new_val)
{
  m_val = new_val;
}


/* Constructor that sets the left and right side bits
   of the bit_complement_expression sign.  */

bit_complement_expression::bit_complement_expression (value_bit *right)
{
  /* As complement has only one argument, we use only the m_right.  */
  this->m_left = nullptr;
  this->m_right = right;
  m_type = BIT_COMPLEMENT_EXPRESSION;
}


/* Copy constructor for bit_complement_expression.  */

bit_complement_expression::bit_complement_expression (
  const bit_complement_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Destructor for bit_expression.  */

bit_expression::~bit_expression ()
{
  delete m_left;
  m_left = nullptr;
  delete m_right;
  m_right = nullptr;
}


/* Returns a copy of the bit.  */

value_bit *
symbolic_bit::copy () const
{
  return new symbolic_bit (*this);
}


/* Return a copy of the bit.  */

value_bit *
bit::copy () const
{
  return new bit (*this);
}


/* Copies the given expression to it by copying the left and right operands.  */

void
bit_expression::copy (const bit_expression *expr)
{
  if (expr->m_left)
    m_left = expr->m_left->copy ();

  if (expr->m_right)
    m_right = expr->m_right->copy ();

  m_type = expr->m_type;
}


/* Returns a copy of the expression.  */

value_bit *
bit_xor_expression::copy () const
{
  return new bit_xor_expression (*this);
}


/* Returns a copy of the expression.  */

value_bit *
bit_and_expression::copy () const
{
  return new bit_and_expression (*this);
}


/* Returns a copy of the expression.  */

value_bit *
bit_or_expression::copy () const
{
  return new bit_or_expression (*this);
}


/* Returns a copy of the expression.  */

value_bit *
shift_right_expression::copy () const
{
  return new shift_right_expression (*this);
}


/* Returns a copy of the expression.  */

value_bit *
shift_left_expression::copy () const
{
  return new shift_left_expression (*this);
}


/* Returns a copy of the expression.  */

value_bit *
add_expression::copy () const
{
  return new add_expression (*this);
}


/* Returns a copy of the expression.  */

value_bit *
sub_expression::copy () const
{
  return new sub_expression (*this);
}


/* Returns a copy of the expression.  */

value_bit *
bit_complement_expression::copy () const
{
  return new bit_complement_expression (*this);
}


/* Constructor that sets the left and right side bits
   of the bit_xor_expression sign.  */

bit_xor_expression::bit_xor_expression (value_bit *left, value_bit *right)
{
  this->m_left = left;
  this->m_right = right;
  m_type = BIT_XOR_EXPRESSION;
}


/* Copy constructor for bit_xor_expression.  */

bit_xor_expression::bit_xor_expression (const bit_xor_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Constructor that sets the left and right side bits
   of the bit_and_expression sign.  */

bit_and_expression::bit_and_expression (value_bit *left, value_bit *right)
{
  this->m_left = left;
  this->m_right = right;
  m_type = BIT_AND_EXPRESSION;
}


/* Copy constructor for bit_and_expression.  */

bit_and_expression::bit_and_expression (const bit_and_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Constructor that sets the left and right side bits
   of the bit_or_expression sign.  */

bit_or_expression::bit_or_expression (value_bit *left, value_bit *right)
{
  this->m_left = left;
  this->m_right = right;
  m_type = BIT_OR_EXPRESSION;
}


/* Copy constructor for bit_or_expression.  */

bit_or_expression::bit_or_expression (const bit_or_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Constructor that sets the left and right side bits
   of the shift_right_expression sign.  */

shift_right_expression::shift_right_expression (value_bit *left,
						value_bit *right)
{
  this->m_left = left;
  this->m_right = right;
  m_type = SHIFT_RIGHT_EXPRESSION;
}


/* Copy constructor for shift_right_expression.  */

shift_right_expression::shift_right_expression (
  const shift_right_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Constructor that sets the left and right side bits
   of the shift_left_expression sign.  */

shift_left_expression::shift_left_expression (value_bit *left, value_bit *right)
{
  this->m_left = left;
  this->m_right = right;
  m_type = SHIFT_LEFT_EXPRESSION;
}


/* Copy constructor for shift_left_expression.  */

shift_left_expression::shift_left_expression (const shift_left_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Constructor that sets the left and right side bits
   of the add_expression sign.  */

add_expression::add_expression (value_bit *left, value_bit *right)
{
  this->m_left = left;
  this->m_right = right;
  m_type = ADD_EXPRESSION;
}


/* Copy constructor for add_expression.  */

add_expression::add_expression (const add_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Constructor that sets the left and right side bits
   of the sub_expression sign.  */

sub_expression::sub_expression (value_bit *left, value_bit *right)
{
  this->m_left = left;
  this->m_right = right;
  m_type = SUB_EXPRESSION;
}


/* Copy constructor for sub_expression.  */

sub_expression::sub_expression (const sub_expression &expr)
{
  bit_expression::copy (&expr);
}


/* Returns the origin of the bit, to whom it belongs.  */

tree
symbolic_bit::get_origin ()
{
  return m_origin;
}


/* Prints the bit.  */

void
symbolic_bit::print ()
{
  if (dump_file)
    {
      print_generic_expr (dump_file, m_origin, dump_flags);
      fprintf (dump_file, "[%zu]", m_index);
    }
}


/* Prints the bit.  */

void
bit::print ()
{
  if (dump_file)
    fprintf (dump_file, "%u", m_val);
}


/* Depending on the expression, prints its sign.  */

void
bit_expression::print_expr_sign ()
{
  switch (m_type)
    {
      case BIT_XOR_EXPRESSION:
	fprintf (dump_file, " ^ ");
	break;
      case BIT_AND_EXPRESSION:
	fprintf (dump_file, " & ");
	break;
      case BIT_OR_EXPRESSION:
	fprintf (dump_file, " | ");
	break;
      case SHIFT_RIGHT_EXPRESSION:
	fprintf (dump_file, " >> ");
	break;
      case SHIFT_LEFT_EXPRESSION:
	fprintf (dump_file, " << ");
	break;
      case ADD_EXPRESSION:
	fprintf (dump_file, " + ");
	break;
      case SUB_EXPRESSION:
	fprintf (dump_file, " - ");
	break;
      default:
	fprintf (dump_file, " ?? ");
    }
}


/* Prints the expression.  */

void
bit_expression::print ()
{
  if (dump_file)
    {
      fprintf (dump_file, "(");
      if (m_left)
	m_left->print ();
      else
	fprintf (dump_file, "null");

      print_expr_sign ();

      if (m_right)
	m_right->print ();
      else
	fprintf (dump_file, "null");

      fprintf (dump_file, ")");
    }
}


/* Prints the expression.  */

void
bit_complement_expression::print ()
{
  if (dump_file)
    {
      fprintf (dump_file, "!");
      if (m_right)
	m_right->print ();
      else
	fprintf (dump_file, "null");
    }
}