1 // -*- mode: C++ -*-
2 //
3 // Copyright (c) 2007, 2008, 2009, 2010, 2011, 2014, 2015, 2017 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 #ifndef SAFEOPFLAGS_H
31 #define SAFEOPFLAGS_H
32 
33 #include <ostream>
34 #include "FunctionInvocation.h"
35 #include "Type.h"
36 
37 enum SafeOpKind {
38 	sOpUnary,
39 	sOpBinary,
40 	sOpAssign,
41 };
42 
43 #define MAX_SAFE_OP_KIND ((SafeOpKind) (sOpAssign+1))
44 
45 enum SafeOpSize {
46 	sInt8,
47 	sInt16,
48 	sInt32,
49 	sInt64,
50 	sFloat,
51 };
52 #define MAX_SAFE_OP_SIZE ((SafeOpSize) (sFloat+1))
53 
54 class SafeOpFlags {
55 public:
56 	static SafeOpFlags *make_random_binary(const Type *rv_type, const Type *op1_type, const Type *op2_type,
57 					SafeOpKind op_kind, eBinaryOps op);
58 
59 	static SafeOpFlags *make_random_unary(const Type *rv_type, const Type *op1_type, eUnaryOps op);
60 
61 	static SafeOpFlags *make_dummy_flags();
62 
63 	static eSimpleType flags_to_type(bool sign, enum SafeOpSize size);
64 
65 	const Type* get_lhs_type(void);
66 	const Type* get_rhs_type(void);
67 
68 	SafeOpFlags *clone() const;
69 
70 	void OutputSize(std::ostream &out) const;
71 
72 	void OutputFuncOrMacro(std::ostream &out) const;
73 
74 	void OutputOp1(std::ostream &out) const;
75 
76 	void OutputOp2(std::ostream &out) const;
77 
get_op1_sign()78 	bool get_op1_sign() { return op1_; }
79 
get_op2_sign()80 	bool get_op2_sign() { return op2_; }
81 
get_op_size()82 	enum SafeOpSize get_op_size() const { return op_size_; }
83 
84 	std::string to_string(enum eBinaryOps op) const;
85 	std::string to_string(enum eUnaryOps  op) const;
86 	static int to_id(std::string fname);
87 
88 	~SafeOpFlags();
89 
90 	static std::vector<std::string> wrapper_names;
91 private:
92 	bool op1_;
93 	bool op2_;
94 	bool is_func_;
95 	SafeOpSize op_size_;
96 
97 	void OutputSign(std::ostream &out, bool sgnd) const;
98 
99 	bool static return_float_type(const Type *rv_type, const Type *op1_type, const Type *op2_type,
100 					eBinaryOps op);
101 
102 	bool static return_float_type(const Type *rv_type, const Type *op1_type,
103 					eUnaryOps uop);
104 
105 	std::string safe_float_func_string(enum eBinaryOps op) const;
106 
107 	SafeOpFlags();
108 
109 	SafeOpFlags(const SafeOpFlags &flags);
110 
111 	SafeOpFlags(bool op1, bool op2, bool is_func, SafeOpSize size);
112 
113 	SafeOpFlags &operator=(const SafeOpFlags &flags); //unimplemented;
114 };
115 
116 #endif //SAFEOPFLAGS_H
117 
118