1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // Eval.cc -- Evaluate parts of the ast at compile time. |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sat Aug 6 12:11:59 2022 |
---|
13 | // Update Count : 119 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Eval.h" |
---|
17 | |
---|
18 | #include <utility> // for pair |
---|
19 | |
---|
20 | #include "AST/Inspect.hpp" |
---|
21 | #include "CodeGen/OperatorTable.h" // access: OperatorInfo |
---|
22 | #include "AST/Pass.hpp" |
---|
23 | #include "InitTweak/InitTweak.h" |
---|
24 | |
---|
25 | struct EvalNew : public ast::WithShortCircuiting { |
---|
26 | Evaluation result = { 0, true, true }; |
---|
27 | |
---|
28 | void previsit( const ast::Node * ) { visit_children = false; } |
---|
29 | void postvisit( const ast::Node * ) { result.isEvaluableInGCC = result.hasKnownValue = false; } |
---|
30 | |
---|
31 | void postvisit( const ast::UntypedExpr * ) { |
---|
32 | assertf( false, "UntypedExpr in constant expression evaluation" ); // FIX ME, resolve variable |
---|
33 | } |
---|
34 | |
---|
35 | void postvisit( const ast::ConstantExpr * expr ) { // only handle int constants |
---|
36 | result.knownValue = expr->intValue(); |
---|
37 | result.hasKnownValue = true; |
---|
38 | result.isEvaluableInGCC = true; |
---|
39 | } |
---|
40 | |
---|
41 | void postvisit( const ast::SizeofExpr * ) { |
---|
42 | result.hasKnownValue = false; |
---|
43 | result.isEvaluableInGCC = true; |
---|
44 | } |
---|
45 | |
---|
46 | void postvisit( const ast::AlignofExpr * ) { |
---|
47 | result.hasKnownValue = false; |
---|
48 | result.isEvaluableInGCC = true; |
---|
49 | } |
---|
50 | |
---|
51 | void postvisit( const ast::OffsetofExpr * ) { |
---|
52 | result.hasKnownValue = false; |
---|
53 | result.isEvaluableInGCC = true; |
---|
54 | } |
---|
55 | |
---|
56 | void postvisit( const ast::LogicalExpr * expr ) { |
---|
57 | Evaluation arg1, arg2; |
---|
58 | arg1 = eval( expr->arg1 ); |
---|
59 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
60 | if ( ! result.isEvaluableInGCC ) return; |
---|
61 | arg2 = eval( expr->arg2 ); |
---|
62 | result.isEvaluableInGCC &= arg2.isEvaluableInGCC; |
---|
63 | if ( ! result.isEvaluableInGCC ) return; |
---|
64 | |
---|
65 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
66 | result.hasKnownValue &= arg2.hasKnownValue; |
---|
67 | if ( ! result.hasKnownValue ) return; |
---|
68 | |
---|
69 | if ( expr->isAnd ) { |
---|
70 | result.knownValue = arg1.knownValue && arg2.knownValue; |
---|
71 | } else { |
---|
72 | result.knownValue = arg1.knownValue || arg2.knownValue; |
---|
73 | } // if |
---|
74 | } |
---|
75 | |
---|
76 | void postvisit( const ast::ConditionalExpr * expr ) { |
---|
77 | Evaluation arg1, arg2, arg3; |
---|
78 | arg1 = eval( expr->arg1 ); |
---|
79 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
80 | if ( ! result.isEvaluableInGCC ) return; |
---|
81 | arg2 = eval( expr->arg2 ); |
---|
82 | result.isEvaluableInGCC &= arg2.isEvaluableInGCC; |
---|
83 | if ( ! result.isEvaluableInGCC ) return; |
---|
84 | arg3 = eval( expr->arg3 ); |
---|
85 | result.isEvaluableInGCC &= arg3.isEvaluableInGCC; |
---|
86 | if ( ! result.isEvaluableInGCC ) return; |
---|
87 | |
---|
88 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
89 | result.hasKnownValue &= arg2.hasKnownValue; |
---|
90 | result.hasKnownValue &= arg3.hasKnownValue; |
---|
91 | if ( ! result.hasKnownValue ) return; |
---|
92 | |
---|
93 | result.knownValue = arg1.knownValue ? arg2.knownValue : arg3.knownValue; |
---|
94 | } |
---|
95 | |
---|
96 | void postvisit( const ast::CastExpr * expr ) { |
---|
97 | // cfa-cc generates a cast before every constant and many other places, e.g., (int)3, |
---|
98 | // so we must use the value from the cast argument, even though we lack any basis for evaluating wraparound effects, etc |
---|
99 | result = eval(expr->arg); |
---|
100 | } |
---|
101 | |
---|
102 | void postvisit( const ast::VariableExpr * expr ) { |
---|
103 | result.hasKnownValue = false; |
---|
104 | result.isEvaluableInGCC = false; |
---|
105 | if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) { |
---|
106 | if ( const ast::EnumDecl * decl = inst->base ) { |
---|
107 | result.isEvaluableInGCC = true; |
---|
108 | result.hasKnownValue = decl->valueOf( expr->var, result.knownValue ); // result.knownValue filled by valueOf |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void postvisit( const ast::ApplicationExpr * expr ) { |
---|
114 | const ast::DeclWithType * function = ast::getFunction(expr); |
---|
115 | if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { |
---|
116 | result.isEvaluableInGCC = false; |
---|
117 | result.hasKnownValue = false; |
---|
118 | return; |
---|
119 | } |
---|
120 | const std::string & fname = function->name; |
---|
121 | assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() ); |
---|
122 | |
---|
123 | if ( expr->args.size() == 1 ) { |
---|
124 | // pre/postfix operators ++ and -- => assignment, which is not constant |
---|
125 | Evaluation arg1; |
---|
126 | arg1 = eval(expr->args.front()); |
---|
127 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
128 | if ( ! result.isEvaluableInGCC ) return; |
---|
129 | |
---|
130 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
131 | if ( ! result.hasKnownValue ) return; |
---|
132 | |
---|
133 | if (fname == "+?") { |
---|
134 | result.knownValue = arg1.knownValue; |
---|
135 | } else if (fname == "-?") { |
---|
136 | result.knownValue = -arg1.knownValue; |
---|
137 | } else if (fname == "~?") { |
---|
138 | result.knownValue = ~arg1.knownValue; |
---|
139 | } else if (fname == "!?") { |
---|
140 | result.knownValue = ! arg1.knownValue; |
---|
141 | } else { |
---|
142 | result.isEvaluableInGCC = false; |
---|
143 | result.hasKnownValue = false; |
---|
144 | } // if |
---|
145 | } else { // => expr->args.size() == 2 |
---|
146 | // infix assignment operators => assignment, which is not constant |
---|
147 | Evaluation arg1, arg2; |
---|
148 | arg1 = eval(expr->args.front()); |
---|
149 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
150 | if ( ! result.isEvaluableInGCC ) return; |
---|
151 | arg2 = eval(expr->args.back()); |
---|
152 | result.isEvaluableInGCC &= arg2.isEvaluableInGCC; |
---|
153 | if ( ! result.isEvaluableInGCC ) return; |
---|
154 | |
---|
155 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
156 | result.hasKnownValue &= arg2.hasKnownValue; |
---|
157 | if ( ! result.hasKnownValue ) return; |
---|
158 | |
---|
159 | if (fname == "?+?") { |
---|
160 | result.knownValue = arg1.knownValue + arg2.knownValue; |
---|
161 | } else if (fname == "?-?") { |
---|
162 | result.knownValue = arg1.knownValue - arg2.knownValue; |
---|
163 | } else if (fname == "?*?") { |
---|
164 | result.knownValue = arg1.knownValue * arg2.knownValue; |
---|
165 | } else if (fname == "?/?") { |
---|
166 | if ( arg2.knownValue ) result.knownValue = arg1.knownValue / arg2.knownValue; |
---|
167 | } else if (fname == "?%?") { |
---|
168 | if ( arg2.knownValue ) result.knownValue = arg1.knownValue % arg2.knownValue; |
---|
169 | } else if (fname == "?<<?") { |
---|
170 | result.knownValue = arg1.knownValue << arg2.knownValue; |
---|
171 | } else if (fname == "?>>?") { |
---|
172 | result.knownValue = arg1.knownValue >> arg2.knownValue; |
---|
173 | } else if (fname == "?<?") { |
---|
174 | result.knownValue = arg1.knownValue < arg2.knownValue; |
---|
175 | } else if (fname == "?>?") { |
---|
176 | result.knownValue = arg1.knownValue > arg2.knownValue; |
---|
177 | } else if (fname == "?<=?") { |
---|
178 | result.knownValue = arg1.knownValue <= arg2.knownValue; |
---|
179 | } else if (fname == "?>=?") { |
---|
180 | result.knownValue = arg1.knownValue >= arg2.knownValue; |
---|
181 | } else if (fname == "?==?") { |
---|
182 | result.knownValue = arg1.knownValue == arg2.knownValue; |
---|
183 | } else if (fname == "?!=?") { |
---|
184 | result.knownValue = arg1.knownValue != arg2.knownValue; |
---|
185 | } else if (fname == "?&?") { |
---|
186 | result.knownValue = arg1.knownValue & arg2.knownValue; |
---|
187 | } else if (fname == "?^?") { |
---|
188 | result.knownValue = arg1.knownValue ^ arg2.knownValue; |
---|
189 | } else if (fname == "?|?") { |
---|
190 | result.knownValue = arg1.knownValue | arg2.knownValue; |
---|
191 | } else { |
---|
192 | result.isEvaluableInGCC = false; |
---|
193 | result.hasKnownValue = false; |
---|
194 | } |
---|
195 | } // if |
---|
196 | // TODO: implement other intrinsic functions |
---|
197 | } |
---|
198 | }; |
---|
199 | |
---|
200 | Evaluation eval( const ast::Expr * expr ) { |
---|
201 | if ( expr ) { |
---|
202 | |
---|
203 | return ast::Pass<EvalNew>::read(expr); |
---|
204 | // Evaluation ret = ast::Pass<EvalNew>::read(expr); |
---|
205 | // ret.knownValue = 777; |
---|
206 | // return ret; |
---|
207 | |
---|
208 | } else { |
---|
209 | return { 0, false, false }; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | // Local Variables: // |
---|
214 | // tab-width: 4 // |
---|
215 | // mode: c++ // |
---|
216 | // compile-command: "make install" // |
---|
217 | // End: // |
---|