-
Notifications
You must be signed in to change notification settings - Fork 140
/
Constant.cpp
executable file
·484 lines (438 loc) · 11 KB
/
Constant.cpp
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
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, 2014 The University of Utah
// All rights reserved.
//
// Copyright (c) 2015-2016 Huawei Technologies Co., Ltd
// All rights reserved.
//
// This file is part of `csmith', a random generator of C programs.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include "Constant.h"
#include <cassert>
#include <ostream>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <climits>
#include "CGContext.h"
#include "Type.h"
#include "Variable.h"
#include "CGOptions.h"
#include "StringUtils.h"
#include "random.h"
using namespace std;
static string GenerateRandomConstant(const Type* type);
///////////////////////////////////////////////////////////////////////////////
/*
* TODO: make well-known constants
*/
///////////////////////////////////////////////////////////////////////////////
/*
*
*/
Constant::Constant(const Type *t, const string &v)
: Expression(eConstant),
type(t),
value(v)
{
}
/*
*
*/
Constant::Constant(const Constant &c)
: Expression(eConstant),
type(c.type),
value(c.value)
{
}
/*
*
*/
Constant::~Constant(void)
{
// Nothing else to do.
}
///////////////////////////////////////////////////////////////////////////////
Expression *
Constant::clone() const
{
return new Constant(*this);
}
// --------------------------------------------------------------
static string
GenerateRandomConstant(int size)
{
string ch;
string suffix = "L";
if (size > 4)
suffix = "LL";
if (CGOptions::ccomp() || !CGOptions::longlong())
ch = string("0x") + RandomHexDigits(2 * size);
else
ch = string("0x") + RandomHexDigits(2 * size) + suffix;
return ch;
}
// --------------------------------------------------------------
#if 0
static string
GenerateRandomFloatConstant(void)
{
string val = RandomDigits(5) + "." + RandomDigits(5);
return val;
}
#endif // 0
/*
* Generate hexadecimal floating point constants [0xF.FFFFFFp-99, 0xF.FFFFFFp+99]
*/
static string
GenerateRandomFloatHexConstant(void)
{
int exp = pure_rnd_upto(100);
ostringstream oss;
oss << "0x" << RandomHexDigits(1) << "." << RandomHexDigits(6) << "p";
if (pure_rnd_flipcoin(50)) {
oss << "+";
}
else {
oss << "-";
}
oss << exp;
return oss.str();
}
/*
* Generate small hexadecimal floating point constants
*/
static string
GenerateSmallRandomFloatHexConstant(int num)
{
ostringstream oss;
if (num >= 0) {
oss << "0x";
}
else {
oss << "-0x";
num = -num;
}
oss << num << "." << RandomHexDigits(1) << "p";
if (pure_rnd_flipcoin(50)) {
oss << "+1";
}
else {
oss << "-1";
}
return oss.str();
}
static string
GenerateRandomConstantInRange(const Type* type, int bound)
{
assert(type->eType == eSimple);
ostringstream oss;
if (type->simple_type == eInt) {
int b = static_cast<int>(pow(2, static_cast<double>(bound) / 2));
int num = pure_rnd_upto(b);
bool flag = pure_rnd_flipcoin(50);
if (flag)
oss << num;
else
oss << "-" << num;
}
else if (type->simple_type == eUInt) {
int b = static_cast<int>(pow(2, static_cast<double>(bound) / 2));
if (b < 0)
b = INT_MAX;
int num = pure_rnd_upto(b);
oss << num;
}
else {
assert(0);
}
return CGOptions::mark_mutable_const() ? "(" + oss.str() + ")" : oss.str();
}
// --------------------------------------------------------------
/* generate a constant struct in the form of
* "{2, 4, {2, 4}, 6.0}"
*************************************************************/
static string
GenerateRandomStructConstant(const Type* t)
{
string value = "{";
size_t i;
assert(t->eType == eStruct);
const AggregateType * type = dynamic_cast<const AggregateType *>(t);
assert(type->fields.size() == type->bitfields_length_.size());
for (i = 0; i < type->fields.size(); i++) {
bool is_bitfield = type->is_bitfield(i);
if (is_bitfield) {
int bound = type->bitfields_length_[i];
if (bound == 0)
continue;
string v = GenerateRandomConstantInRange(type->fields[i], bound);
if (i > 0) {
value += ",";
}
value += v;
}
else {
string v = GenerateRandomConstant(type->fields[i]);
if (i > 0) {
value += ",";
}
value += v;
}
}
value += "}";
return value;
}
// --------------------------------------------------------------
/* generate an union initializer: unlike struct, initializing
the first field is enough
*************************************************************/
static string
GenerateRandomUnionConstant(const Type* t)
{
string value = "{";
assert (t->eType == eUnion);
const AggregateType * type = dynamic_cast<const AggregateType *>(t);
assert(type->fields.size() == type->bitfields_length_.size());
value += GenerateRandomConstant(type->fields[0]);
value += "}";
return value;
}
static string
GenerateRandomConstant(const Type* type)
{
string v;
if (type == 0) {
v = "0";
}
else if (type->eType == eStruct) {
v = GenerateRandomStructConstant(type);
}
else if (type->eType == eUnion) {
v = GenerateRandomUnionConstant(type);
}
// the only possible constant for a pointer is "0"
else if (type->eType == ePointer) {
v = "0";
}
else if (type->eType == eSimple) {
eSimpleType st = type->simple_type;
assert(st != eVoid);
//assert((eType >= 0) && (eType <= MAX_SIMPLE_TYPES));
if (pure_rnd_flipcoin(50)) {
int num = 0;
if (pure_rnd_flipcoin(50)) {
num = pure_rnd_upto(3)-1;
} else {
num = pure_rnd_upto(20)-10;
}
// don't use negative number for unsigned type, as this causes
//trouble for some static analyzers
ostringstream oss;
switch (st) {
case eUChar:
oss << (unsigned int)(unsigned char)num;
break;
case eUShort:
oss << (unsigned short)num;
break;
case eUInt:
oss << (unsigned int)num;
break;
case eULong:
case eULongLong:
if (!CGOptions::longlong()) {
oss << (unsigned int)num;
} else {
oss << ((type->simple_type == eULong) ? (unsigned long)num : (unsigned INT64)num);
}
break;
case eFloat:
oss << GenerateSmallRandomFloatHexConstant(num);
break;
default:
oss << num;
break;
}
if (type->simple_type == eFloat) {
v = oss.str();
}
else {
if (CGOptions::ccomp() || !CGOptions::longlong())
v = oss.str() + (type->is_signed() ? "" : "U");
else
v = oss.str() + (type->is_signed() ? "L" : "UL");
}
} else {
if(st == eVoid)
v = "/* void */";
else
v = GenerateRandomConstant(Type::get_simple_type(st).SizeInBytes());
}
} else {
assert(0); // no support for types other than integers and structs for now
}
return (type->eType == eSimple && CGOptions::mark_mutable_const()) ? "(" + v + ")" : v;
}
// --------------------------------------------------------------
/*
* Sometimes we need to generate constants outside of any current function,
* e.g., initializers for global variables
*/
Constant *
Constant::make_random(const Type* type)
{
string v = GenerateRandomConstant(type);
return new Constant(type, v);
}
Constant *
Constant::make_random_upto(unsigned int limit)
{
ostringstream oss;
oss << rnd_upto(limit);
return new Constant(&Type::get_simple_type(eUInt), oss.str());
}
Constant*
Constant::make_random_nonzero(const Type* type)
{
string v = GenerateRandomConstant(type);
while (StringUtils::str2int(v) == 0) {
v = GenerateRandomConstant(type);
}
return new Constant(type, v);
}
/*
* Return a `Constant' representing the integer value `v'. Note that the
* return value may not be "fresh."
*/
Constant *
Constant::make_int(int v)
{
// Commented out code for the cache:
// It's hard for releasint the memory for those cached Constants.
#if 0
static const int cache_size = 256;
static bool cache_inited = false;
static Constant *cache_constants[cache_size];
#endif
const Type &int_type = Type::get_simple_type(eInt);
#if 0
// Initialize our cache of small-number constants.
if (!cache_inited) {
cache_inited = true;
ostringstream oss;
for (int i = 0; i < cache_size; ++i) {
oss.str("");
oss << i;
cache_constants[i] = new Constant(&int_type, oss.str());
}
}
// Get small numbers out of the cache.
if ((v >= 0) && (v < cache_size)) {
return cache_constants[v];
}
#endif
// Create fresh constants for values outside of our cache limit.
ostringstream oss;
oss << v;
string s = CGOptions::mark_mutable_const() ? ("(" + oss.str() + ")") : oss.str();
return new Constant(&int_type, s);
}
bool
Constant::compatible(const Variable *v) const
{
assert(v);
if (CGOptions::expand_struct())
return true;
//return !v->is_field_var();
return false;
}
bool
Constant::compatible(const Expression *exp) const
{
assert(exp);
return false;
}
bool
Constant::less_than(int num) const
{
return StringUtils::str2int(value) < num;
}
bool
Constant::not_equals(int num) const
{
return StringUtils::str2int(value) != num;
}
bool
Constant::equals(int num) const
{
return StringUtils::str2int(value) == num;
}
string
Constant::get_field(size_t fid) const
{
vector<string> fields;
StringUtils::split_string(value, fields, "{},");
if (fid < fields.size()) {
return fields[fid];
}
return "";
}
///////////////////////////////////////////////////////////////////////////////
/*
*
*/
const Type &
Constant::get_type(void) const
{
return *(type);
}
/*
*
*/
void
Constant::Output(std::ostream &out) const
{
output_cast(out);
//enclose negative numbers in parenthesis to avoid syntax errors such as "--8"
if (!value.empty() && value[0] == '-') {
out << "(" << value << ")";
} else if (type->eType == ePointer && equals(0)){
if (CGOptions::lang_cpp()) {
out << "NULL";
} else {
out << "(void*)" << value;
}
} else {
out << value;
}
}
///////////////////////////////////////////////////////////////////////////////
// Local Variables:
// c-basic-offset: 4
// tab-width: 4
// End:
// End of file.