1 // -*- mode: C++ -*-
2 //
3 // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2014, 2015 The University of Utah
4 // All rights reserved.
5 //
6 // This file is part of `csmith', a random generator of C programs.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 //   * Redistributions of source code must retain the above copyright notice,
12 //     this list of conditions and the following disclaimer.
13 //
14 //   * Redistributions in binary form must reproduce the above copyright
15 //     notice, this list of conditions and the following disclaimer in the
16 //     documentation and/or other materials provided with the distribution.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 
30 //
31 // This file was derived from a random program generator written by Bryan
32 // Turner.  The attributions in that file was:
33 //
34 // Random Program Generator
35 // Bryan Turner (bryan.turner@pobox.com)
36 // July, 2005
37 //
38 
39 #ifndef FUNCTION_INVOCATION_H
40 #define FUNCTION_INVOCATION_H
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #include <ostream>
45 #include <vector>
46 #include "util.h"
47 #include "CVQualifiers.h"
48 using namespace std;
49 
50 class CGContext;
51 class Function;
52 class Expression;
53 class FunctionInvocationUser;
54 class Type;
55 class Fact;
56 class SafeOpFlags;
57 class Variable;
58 class CVQualifiers;
59 
60 enum eUnaryOps
61 {
62 	ePlus,
63 	eMinus,
64 	eNot,
65 	eBitNot
66 #if 0 // TODO --- to be implemented
67 	ePreInc,
68 	ePreDec,
69 	ePostInc,
70 	ePostDec
71 #endif // 0
72 };
73 #define MAX_UNARY_OP ((eUnaryOps) (eBitNot+1))
74 #if 0 // TODO --- to be implemented
75 #  define MAX_UNARY_OP ((eUnaryOps) (ePostDec+1))
76 #endif
77 
78 enum eBinaryOps
79 {
80 	eAdd,
81 	eSub,
82 	eMul,
83 	eDiv,
84 	eMod,
85 	eCmpGt,
86 	eCmpLt,
87 	eCmpGe,
88 	eCmpLe,
89 	eCmpEq,
90 	eCmpNe,
91 	eAnd,
92 	eOr,
93 	eBitXor,
94 	eBitAnd,
95 	eBitOr,
96 	eRShift,
97 	eLShift
98 };
99 #define MAX_BINARY_OP ((eBinaryOps) (eLShift+1))
100 
101 enum eInvocationType
102 {
103     eBinaryPrim,
104     eUnaryPrim,
105     eFuncCall
106 };
107 
108 class FunctionInvocation
109 {
110 public:
111 	FunctionInvocation(eInvocationType e, const SafeOpFlags *flags);
112 
113 	virtual ~FunctionInvocation(void);
114 
115 	virtual FunctionInvocation *clone() const = 0;
116 
117 	static FunctionInvocation *make_random(bool,
118 										   CGContext &cg_context,
119                                            const Type* type,
120 										   const CVQualifiers* qfer);
121 	static FunctionInvocation *make_random(Function *target,
122 										   CGContext &cg_context);
123 
124 	static FunctionInvocation *make_random_unary(CGContext &cg_context, const Type* type);
125 
126 	static FunctionInvocation *make_random_binary(CGContext &cg_context, const Type* type);
127 
128 	static FunctionInvocation * make_random_binary_ptr_comparison(CGContext &cg_context);
129 
130 	static FunctionInvocation *make_unary(CGContext &cg_context, eUnaryOps op,
131 										  Expression *operand);
132 	static FunctionInvocation *make_binary(CGContext &cg_context, eBinaryOps op,
133 										   Expression *lhs,
134 										   Expression *rhs);
135 
136 	virtual bool visit_facts(vector<const Fact*>& inputs, CGContext& cg_context) const;
137 
138 	virtual void get_called_funcs(std::vector<const FunctionInvocationUser*>& funcs ) const;
139 
140 	virtual bool has_uncertain_call(void) const;
141 
142 	vector<intvec> permute_param_oders(void) const;
143 
144 	bool visit_unordered_params(vector<const Fact*>& inputs, CGContext& cg_context) const;
145 
146 	bool has_uncertain_call_recursive(void) const;
147 
148 	bool has_simple_params(void) const;
149 
150 	CVQualifiers get_qualifiers(void) const;
151 
152 	void add_operand(const Expression* e);
153 
154 	static bool IsOrderedStandardFunc(eBinaryOps eFunc);
155 
156 	static bool BinaryOpWorksForFloat(eBinaryOps op);
157 
158 	static bool UnaryOpWorksForFloat(eUnaryOps op);
159 
160 	virtual const Type &get_type(void) const = 0;
161 
compatible(const Variable *)162 	virtual bool compatible(const Variable *) const { return false; }
163 
is_0_or_1(void)164 	virtual bool is_0_or_1(void) const { return false;}
165 
equals(int)166 	virtual bool equals(int /*num*/) const { return false;}
167 
168 	virtual void Output(std::ostream &) const = 0;
169 
170 	virtual void indented_output(std::ostream &out, int indent) const = 0;
171 
172 	virtual bool safe_invocation() const = 0;
173 
174 	eInvocationType invoke_type;
175 
176 	std::vector<const Expression*> param_value;
177 
178 	bool failed;	// indicates whether this invocation has failed to pass pointer/effect analysis
179 	bool ptr_cmp;	// indicates whether this is a pointer comparison
180 protected:
181 	explicit FunctionInvocation(const FunctionInvocation &fi);
182 
183 	const SafeOpFlags *op_flags;
184 
185 private:
186 	// unimplemented
187 	FunctionInvocation &operator=(const FunctionInvocation &fi);
188 };
189 
190 ///////////////////////////////////////////////////////////////////////////////
191 
192 #endif // FUNCTION_INVOCATION_H
193 
194 // Local Variables:
195 // c-basic-offset: 4
196 // tab-width: 4
197 // End:
198 
199 // End of file.
200