forked from csmith-project/csmith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression.cpp
345 lines (310 loc) · 9.29 KB
/
Expression.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
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2010, 2011, 2013, 2015, 2017 The University of Utah
// 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.
//
// This file was derived from a random program generator written by Bryan
// Turner. The attributions in that file was:
//
// Random Program Generator
// Bryan Turner ([email protected])
// July, 2005
//
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "Expression.h"
#include <cassert>
#include "Common.h"
#include "CGContext.h"
#include "CGOptions.h"
#include "Effect.h"
#include "Function.h"
#include "VectorFilter.h"
#include "Variable.h"
#include "Constant.h" // temporary; don't want to depend on subclass!
#include "ExpressionFuncall.h" // temporary; don't want to depend on subclass!
#include "ExpressionVariable.h" // temporary; don't want to depend on subclass!
#include "ExpressionAssign.h"
#include "ExpressionComma.h"
#include "Error.h"
#include "ProbabilityTable.h"
#include "PartialExpander.h"
#include "random.h"
#include "CVQualifiers.h"
#include "DepthSpec.h"
int eid = 0;
DistributionTable Expression::exprTable_;
DistributionTable Expression::paramTable_;
void
Expression::InitExprProbabilityTable()
{
exprTable_.add_entry((int)eFunction, 70);
exprTable_.add_entry((int)eVariable, 20);
exprTable_.add_entry((int)eConstant, 10);
if (CGOptions::use_embedded_assigns()) {
exprTable_.add_entry((int)eAssignment, 10);
}
if (CGOptions::use_comma_exprs()) {
exprTable_.add_entry((int)eCommaExpr, 10);
}
}
void
Expression::InitParamProbabilityTable()
{
paramTable_.add_entry((int)eFunction, 40);
paramTable_.add_entry((int)eVariable, 40);
// constant parameters lead to non-interesting code
paramTable_.add_entry((int)eConstant, 0);
if (CGOptions::use_embedded_assigns()) {
paramTable_.add_entry((int)eAssignment, 10);
}
if (CGOptions::use_comma_exprs()) {
paramTable_.add_entry((int)eCommaExpr, 10);
}
}
void
Expression::InitProbabilityTables()
{
Expression::InitExprProbabilityTable();
Expression::InitParamProbabilityTable();
}
///////////////////////////////////////////////////////////////////////////////
/*
*
*/
static eTermType
ExpressionTypeProbability(const VectorFilter *filter)
{
if (PartialExpander::direct_expand_check(eInvoke))
return eFunction;
assert(filter);
int i = rnd_upto(filter->get_max_prob(), filter);
ERROR_GUARD(MAX_TERM_TYPES);
return (eTermType)(filter->lookup(i));
}
unsigned int
Expression::func_count(void) const
{
std::vector<const FunctionInvocationUser*> funcs;
get_called_funcs(funcs);
return funcs.size();
}
std::string
Expression::to_string(void) const
{
ostringstream oss;
Output(oss);
return oss.str();
}
std::vector<const ExpressionVariable*>
Expression::get_dereferenced_ptrs(void) const
{
// return a empty vector by default
std::vector<const ExpressionVariable*> empty;
return empty;
}
void
Expression::indented_output(std::ostream &out, int indent) const
{
output_tab(out, indent);
Output(out);
}
/*
*
*/
Expression *
Expression::make_random(CGContext &cg_context, const Type* type, const CVQualifiers* qfer, bool no_func, bool no_const, enum eTermType tt)
{
DEPTH_GUARD_BY_TYPE_RETURN_WITH_FLAG(dtExpression, tt, NULL);
Expression *e = 0;
if (type == NULL) {
do {
type = cg_context.get_effect_context().is_side_effect_free() ? Type::choose_random_nonvoid() : Type::choose_random_nonvoid_nonvolatile();
} while (type->eType == eStruct && tt == eConstant);
}
assert(!(no_func && tt == eFunction));
assert(!(no_const && tt == eConstant));
// constant struct variables can not be a subexpression?
assert(!(type->eType == eStruct && tt == eConstant));
// if no term type is provided, choose a random term type with restrictions
if (tt == MAX_TERM_TYPES) {
VectorFilter filter(&Expression::exprTable_);
if (no_func ||
(!CGOptions::return_structs() && type->eType == eStruct) ||
(!CGOptions::return_unions() && type->eType == eUnion)) {
filter.add(eFunction);
}
// struct constants can't be subexpressions (union constant can't either?)
if (no_const || type->eType == eStruct || type->eType == eUnion) {
filter.add(eConstant);
}
// can't assign to constant struct/unions. on the other hand, assign to a volatile
// struct/union cause too much trouble for effect analysis, disable it for now
if (type->is_const_struct_union() || type->is_volatile_struct_union()) {
filter.add(eAssignment);
}
if (cg_context.expr_depth + 2 > CGOptions::max_expr_depth()) {
filter.add(eFunction).add(eAssignment).add(eCommaExpr);
}
tt = ExpressionTypeProbability(&filter);
}
ERROR_GUARD(NULL);
switch (tt) {
case eConstant:
if (type->eType == eSimple)
assert(type->simple_type != eVoid);
e = Constant::make_random(type);
break;
case eVariable:
e = ExpressionVariable::make_random(cg_context, type, qfer);
break;
case eFunction:
e = ExpressionFuncall::make_random(cg_context, type, qfer);
break;
case eAssignment:
e = ExpressionAssign::make_random(cg_context, type, qfer);
break;
case eCommaExpr:
e = ExpressionComma::make_random(cg_context, type, qfer);
break;
default: break;
}
#if 0
if (!cg_context.get_effect_context().is_side_effect_free()) {
assert(e->effect.is_side_effect_free());
}
#endif
// increment expression depth. A function call increase the depth by 1
if (e->term_type == eConstant || e->term_type == eVariable ||
(e->get_invoke() && e->get_invoke()->invoke_type == eFuncCall)) {
cg_context.expr_depth++;
}
ERROR_GUARD(NULL);
return e;
}
void
Expression::check_and_set_cast(const Type* type)
{
if(CGOptions::lang_cpp() && get_type().needs_cast(type)) {
cast_type = type;
}
}
void
Expression::output_cast(std::ostream& out) const
{
if((CGOptions::ccomp() || CGOptions::lang_cpp()) && (cast_type != NULL)) {
out << "(";
cast_type->Output(out);
out << ") ";
}
}
/*
*
*/
Expression *
Expression::make_random_param(CGContext &cg_context, const Type* type, const CVQualifiers* qfer, enum eTermType tt)
{
DEPTH_GUARD_BY_TYPE_RETURN_WITH_FLAG(dtExpressionRandomParam, tt, NULL);
Expression *e = 0;
assert(type);
// if a term type is provided, no need to choose random term type
if (tt == MAX_TERM_TYPES) {
VectorFilter filter(&Expression::paramTable_);
filter.add(eConstant); // don't call functions with constant parameters because it is not interesting
if ((!CGOptions::return_structs() && type->eType == eStruct) ||
(!CGOptions::return_unions() && type->eType == eUnion)) {
filter.add(eFunction);
}
if (type->is_const_struct_union()) {
filter.add(eAssignment);
}
if (cg_context.expr_depth + 2 > CGOptions::max_expr_depth()) {
filter.add(eFunction).add(eAssignment).add(eCommaExpr);
}
//??? Debug by Ben Hu, param should be a variable, if parameter is struct
if (type->eType == eStruct || type->eType == eUnion) {
filter.add(eFunction).add(eAssignment).add(eCommaExpr);
}
tt = ExpressionTypeProbability(&filter);
}
ERROR_GUARD(NULL);
switch (tt) {
case eConstant:
if (type->eType == eSimple)
assert(type->simple_type != eVoid);
e = Constant::make_random(type);
break;
case eVariable:
e = ExpressionVariable::make_random(cg_context, type, qfer, true);
break;
case eFunction:
e = ExpressionFuncall::make_random(cg_context, type, qfer);
break;
case eAssignment:
e = ExpressionAssign::make_random(cg_context, type, qfer);
break;
case eCommaExpr:
e = ExpressionComma::make_random(cg_context, type, qfer);
break;
default: break;
}
if (e->term_type == eConstant || e->term_type == eVariable ||
(e->get_invoke() && e->get_invoke()->invoke_type == eFuncCall)) {
cg_context.expr_depth++;
}
ERROR_GUARD(NULL);
return e;
}
/*
*
*/
Expression::Expression(eTermType e) :
term_type(e),
expr_id(eid++),
cast_type(NULL)
{
// Nothing to do.
}
Expression::Expression(const Expression &expr) :
term_type(expr.term_type),
cast_type(NULL)
{
}
/*
*
*/
Expression::~Expression(void)
{
// Nothing to do.
}
///////////////////////////////////////////////////////////////////////////////
// Local Variables:
// c-basic-offset: 4
// tab-width: 4
// End:
// End of file.