[5cbacf1] | 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 | // |
---|
[8f06277] | 7 | // Eval.cc -- Evaluate parts of the ast at compile time. |
---|
[5cbacf1] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[ba48a9b] | 12 | // Last Modified On : Sat Aug 6 12:11:59 2022 |
---|
| 13 | // Update Count : 119 |
---|
[5cbacf1] | 14 | // |
---|
| 15 | |
---|
[8f06277] | 16 | #include "Eval.h" |
---|
| 17 | |
---|
[5cbacf1] | 18 | #include <utility> // for pair |
---|
| 19 | |
---|
[e01eb4a] | 20 | #include "AST/Inspect.hpp" |
---|
[5cbacf1] | 21 | #include "Common/PassVisitor.h" |
---|
[120867e] | 22 | #include "CodeGen/OperatorTable.h" // access: OperatorInfo |
---|
[d7aa12c] | 23 | #include "AST/Pass.hpp" |
---|
[5cbacf1] | 24 | #include "InitTweak/InitTweak.h" |
---|
| 25 | #include "SynTree/Expression.h" |
---|
| 26 | |
---|
[d7aa12c] | 27 | //------------------------------------------------------------- |
---|
| 28 | // Old AST |
---|
| 29 | struct EvalOld : public WithShortCircuiting { |
---|
[120867e] | 30 | long long int value = 0; // compose the result of the constant expression |
---|
| 31 | bool valid = true; // true => constant expression and value is the result |
---|
| 32 | // false => not constant expression, e.g., ++i |
---|
| 33 | bool cfavalid = true; // true => constant expression and value computable |
---|
| 34 | // false => constant expression but value not computable, e.g., sizeof(int) |
---|
[5cbacf1] | 35 | |
---|
[77d2432] | 36 | void previsit( const BaseSyntaxNode * ) { visit_children = false; } |
---|
| 37 | void postvisit( const BaseSyntaxNode * ) { valid = false; } |
---|
[5cbacf1] | 38 | |
---|
[77d2432] | 39 | void postvisit( const SizeofExpr * ) { |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | void postvisit( const ConstantExpr * expr ) { |
---|
[5cbacf1] | 43 | value = expr->intValue(); |
---|
| 44 | } |
---|
| 45 | |
---|
[77d2432] | 46 | void postvisit( const CastExpr * expr ) { |
---|
[5cbacf1] | 47 | auto arg = eval(expr->arg); |
---|
| 48 | valid = arg.second; |
---|
| 49 | value = arg.first; |
---|
| 50 | // TODO: perform type conversion on value if valid |
---|
| 51 | } |
---|
| 52 | |
---|
[77d2432] | 53 | void postvisit( const VariableExpr * const expr ) { |
---|
[5cbacf1] | 54 | if ( EnumInstType * inst = dynamic_cast<EnumInstType *>(expr->result) ) { |
---|
| 55 | if ( EnumDecl * decl = inst->baseEnum ) { |
---|
| 56 | if ( decl->valueOf( expr->var, value ) ) { // value filled by valueOf |
---|
| 57 | return; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | valid = false; |
---|
| 62 | } |
---|
| 63 | |
---|
[77d2432] | 64 | void postvisit( const ApplicationExpr * expr ) { |
---|
| 65 | DeclarationWithType * function = InitTweak::getFunction(const_cast<ApplicationExpr *>(expr)); |
---|
[5cbacf1] | 66 | if ( ! function || function->linkage != LinkageSpec::Intrinsic ) { valid = false; return; } |
---|
| 67 | const std::string & fname = function->name; |
---|
| 68 | assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() ); |
---|
| 69 | std::pair<long long int, bool> arg1, arg2; |
---|
| 70 | arg1 = eval(expr->args.front()); |
---|
| 71 | valid = valid && arg1.second; |
---|
| 72 | if ( ! valid ) return; |
---|
| 73 | if ( expr->args.size() == 2 ) { |
---|
| 74 | arg2 = eval(expr->args.back()); |
---|
| 75 | valid = valid && arg2.second; |
---|
| 76 | if ( ! valid ) return; |
---|
| 77 | } |
---|
| 78 | if (fname == "?+?") { |
---|
| 79 | value = arg1.first + arg2.first; |
---|
| 80 | } else if (fname == "?-?") { |
---|
| 81 | value = arg1.first - arg2.first; |
---|
| 82 | } else if (fname == "?*?") { |
---|
| 83 | value = arg1.first * arg2.first; |
---|
| 84 | } else if (fname == "?/?") { |
---|
| 85 | value = arg1.first / arg2.first; |
---|
| 86 | } else if (fname == "?%?") { |
---|
| 87 | value = arg1.first % arg2.first; |
---|
| 88 | } else { |
---|
| 89 | valid = false; |
---|
| 90 | } |
---|
| 91 | // TODO: implement other intrinsic functions |
---|
| 92 | } |
---|
| 93 | }; |
---|
| 94 | |
---|
[d7aa12c] | 95 | //------------------------------------------------------------- |
---|
| 96 | // New AST |
---|
| 97 | struct EvalNew : public ast::WithShortCircuiting { |
---|
[8f557161] | 98 | Evaluation result = { 0, true, true }; |
---|
[d7aa12c] | 99 | |
---|
| 100 | void previsit( const ast::Node * ) { visit_children = false; } |
---|
[8f557161] | 101 | void postvisit( const ast::Node * ) { result.isEvaluableInGCC = result.hasKnownValue = false; } |
---|
[d7aa12c] | 102 | |
---|
[120867e] | 103 | void postvisit( const ast::UntypedExpr * ) { |
---|
| 104 | assertf( false, "UntypedExpr in constant expression evaluation" ); // FIX ME, resolve variable |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void postvisit( const ast::ConstantExpr * expr ) { // only handle int constants |
---|
[8f557161] | 108 | result.knownValue = expr->intValue(); |
---|
| 109 | result.hasKnownValue = true; |
---|
| 110 | result.isEvaluableInGCC = true; |
---|
[d7aa12c] | 111 | } |
---|
| 112 | |
---|
[120867e] | 113 | void postvisit( const ast::SizeofExpr * ) { |
---|
[8f557161] | 114 | result.hasKnownValue = false; |
---|
| 115 | result.isEvaluableInGCC = true; |
---|
[120867e] | 116 | } |
---|
| 117 | |
---|
| 118 | void postvisit( const ast::AlignofExpr * ) { |
---|
[8f557161] | 119 | result.hasKnownValue = false; |
---|
| 120 | result.isEvaluableInGCC = true; |
---|
[120867e] | 121 | } |
---|
| 122 | |
---|
| 123 | void postvisit( const ast::OffsetofExpr * ) { |
---|
[8f557161] | 124 | result.hasKnownValue = false; |
---|
| 125 | result.isEvaluableInGCC = true; |
---|
[77d2432] | 126 | } |
---|
| 127 | |
---|
[120867e] | 128 | void postvisit( const ast::LogicalExpr * expr ) { |
---|
[8f557161] | 129 | Evaluation arg1, arg2; |
---|
[120867e] | 130 | arg1 = eval( expr->arg1 ); |
---|
[8f557161] | 131 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 132 | if ( ! result.isEvaluableInGCC ) return; |
---|
[120867e] | 133 | arg2 = eval( expr->arg2 ); |
---|
[8f557161] | 134 | result.isEvaluableInGCC &= arg2.isEvaluableInGCC; |
---|
| 135 | if ( ! result.isEvaluableInGCC ) return; |
---|
| 136 | |
---|
| 137 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
| 138 | result.hasKnownValue &= arg2.hasKnownValue; |
---|
| 139 | if ( ! result.hasKnownValue ) return; |
---|
[120867e] | 140 | |
---|
| 141 | if ( expr->isAnd ) { |
---|
[8f557161] | 142 | result.knownValue = arg1.knownValue && arg2.knownValue; |
---|
[120867e] | 143 | } else { |
---|
[8f557161] | 144 | result.knownValue = arg1.knownValue || arg2.knownValue; |
---|
[120867e] | 145 | } // if |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void postvisit( const ast::ConditionalExpr * expr ) { |
---|
[8f557161] | 149 | Evaluation arg1, arg2, arg3; |
---|
[120867e] | 150 | arg1 = eval( expr->arg1 ); |
---|
[8f557161] | 151 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 152 | if ( ! result.isEvaluableInGCC ) return; |
---|
[120867e] | 153 | arg2 = eval( expr->arg2 ); |
---|
[8f557161] | 154 | result.isEvaluableInGCC &= arg2.isEvaluableInGCC; |
---|
| 155 | if ( ! result.isEvaluableInGCC ) return; |
---|
[120867e] | 156 | arg3 = eval( expr->arg3 ); |
---|
[8f557161] | 157 | result.isEvaluableInGCC &= arg3.isEvaluableInGCC; |
---|
| 158 | if ( ! result.isEvaluableInGCC ) return; |
---|
| 159 | |
---|
| 160 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
| 161 | result.hasKnownValue &= arg2.hasKnownValue; |
---|
| 162 | result.hasKnownValue &= arg3.hasKnownValue; |
---|
| 163 | if ( ! result.hasKnownValue ) return; |
---|
[120867e] | 164 | |
---|
[8f557161] | 165 | result.knownValue = arg1.knownValue ? arg2.knownValue : arg3.knownValue; |
---|
[120867e] | 166 | } |
---|
| 167 | |
---|
| 168 | void postvisit( const ast::CastExpr * expr ) { |
---|
[8f557161] | 169 | // cfa-cc generates a cast before every constant and many other places, e.g., (int)3, |
---|
| 170 | // so we must use the value from the cast argument, even though we lack any basis for evaluating wraparound effects, etc |
---|
| 171 | result = eval(expr->arg); |
---|
[d7aa12c] | 172 | } |
---|
| 173 | |
---|
[120867e] | 174 | void postvisit( const ast::VariableExpr * expr ) { |
---|
[8f557161] | 175 | result.hasKnownValue = false; |
---|
| 176 | result.isEvaluableInGCC = false; |
---|
[d7aa12c] | 177 | if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) { |
---|
| 178 | if ( const ast::EnumDecl * decl = inst->base ) { |
---|
[8f557161] | 179 | result.isEvaluableInGCC = true; |
---|
| 180 | result.hasKnownValue = decl->valueOf( expr->var, result.knownValue ); // result.knownValue filled by valueOf |
---|
[d7aa12c] | 181 | } |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void postvisit( const ast::ApplicationExpr * expr ) { |
---|
[e01eb4a] | 186 | const ast::DeclWithType * function = ast::getFunction(expr); |
---|
[8f557161] | 187 | if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { |
---|
| 188 | result.isEvaluableInGCC = false; |
---|
| 189 | result.hasKnownValue = false; |
---|
| 190 | return; |
---|
| 191 | } |
---|
[d7aa12c] | 192 | const std::string & fname = function->name; |
---|
| 193 | assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() ); |
---|
[120867e] | 194 | |
---|
| 195 | if ( expr->args.size() == 1 ) { |
---|
| 196 | // pre/postfix operators ++ and -- => assignment, which is not constant |
---|
[8f557161] | 197 | Evaluation arg1; |
---|
[120867e] | 198 | arg1 = eval(expr->args.front()); |
---|
[8f557161] | 199 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 200 | if ( ! result.isEvaluableInGCC ) return; |
---|
| 201 | |
---|
| 202 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
| 203 | if ( ! result.hasKnownValue ) return; |
---|
[120867e] | 204 | |
---|
| 205 | if (fname == "+?") { |
---|
[8f557161] | 206 | result.knownValue = arg1.knownValue; |
---|
[120867e] | 207 | } else if (fname == "-?") { |
---|
[8f557161] | 208 | result.knownValue = -arg1.knownValue; |
---|
[120867e] | 209 | } else if (fname == "~?") { |
---|
[8f557161] | 210 | result.knownValue = ~arg1.knownValue; |
---|
[120867e] | 211 | } else if (fname == "!?") { |
---|
[8f557161] | 212 | result.knownValue = ! arg1.knownValue; |
---|
[120867e] | 213 | } else { |
---|
[8f557161] | 214 | result.isEvaluableInGCC = false; |
---|
| 215 | result.hasKnownValue = false; |
---|
[120867e] | 216 | } // if |
---|
| 217 | } else { // => expr->args.size() == 2 |
---|
| 218 | // infix assignment operators => assignment, which is not constant |
---|
[8f557161] | 219 | Evaluation arg1, arg2; |
---|
[120867e] | 220 | arg1 = eval(expr->args.front()); |
---|
[8f557161] | 221 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 222 | if ( ! result.isEvaluableInGCC ) return; |
---|
[d7aa12c] | 223 | arg2 = eval(expr->args.back()); |
---|
[8f557161] | 224 | result.isEvaluableInGCC &= arg2.isEvaluableInGCC; |
---|
| 225 | if ( ! result.isEvaluableInGCC ) return; |
---|
| 226 | |
---|
| 227 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
| 228 | result.hasKnownValue &= arg2.hasKnownValue; |
---|
| 229 | if ( ! result.hasKnownValue ) return; |
---|
[120867e] | 230 | |
---|
| 231 | if (fname == "?+?") { |
---|
[8f557161] | 232 | result.knownValue = arg1.knownValue + arg2.knownValue; |
---|
[120867e] | 233 | } else if (fname == "?-?") { |
---|
[8f557161] | 234 | result.knownValue = arg1.knownValue - arg2.knownValue; |
---|
[120867e] | 235 | } else if (fname == "?*?") { |
---|
[8f557161] | 236 | result.knownValue = arg1.knownValue * arg2.knownValue; |
---|
[120867e] | 237 | } else if (fname == "?/?") { |
---|
[8f557161] | 238 | if ( arg2.knownValue ) result.knownValue = arg1.knownValue / arg2.knownValue; |
---|
[120867e] | 239 | } else if (fname == "?%?") { |
---|
[8f557161] | 240 | if ( arg2.knownValue ) result.knownValue = arg1.knownValue % arg2.knownValue; |
---|
[120867e] | 241 | } else if (fname == "?<<?") { |
---|
[8f557161] | 242 | result.knownValue = arg1.knownValue << arg2.knownValue; |
---|
[120867e] | 243 | } else if (fname == "?>>?") { |
---|
[8f557161] | 244 | result.knownValue = arg1.knownValue >> arg2.knownValue; |
---|
[120867e] | 245 | } else if (fname == "?<?") { |
---|
[8f557161] | 246 | result.knownValue = arg1.knownValue < arg2.knownValue; |
---|
[120867e] | 247 | } else if (fname == "?>?") { |
---|
[8f557161] | 248 | result.knownValue = arg1.knownValue > arg2.knownValue; |
---|
[120867e] | 249 | } else if (fname == "?<=?") { |
---|
[8f557161] | 250 | result.knownValue = arg1.knownValue <= arg2.knownValue; |
---|
[120867e] | 251 | } else if (fname == "?>=?") { |
---|
[8f557161] | 252 | result.knownValue = arg1.knownValue >= arg2.knownValue; |
---|
[120867e] | 253 | } else if (fname == "?==?") { |
---|
[8f557161] | 254 | result.knownValue = arg1.knownValue == arg2.knownValue; |
---|
[120867e] | 255 | } else if (fname == "?!=?") { |
---|
[8f557161] | 256 | result.knownValue = arg1.knownValue != arg2.knownValue; |
---|
[120867e] | 257 | } else if (fname == "?&?") { |
---|
[8f557161] | 258 | result.knownValue = arg1.knownValue & arg2.knownValue; |
---|
[120867e] | 259 | } else if (fname == "?^?") { |
---|
[8f557161] | 260 | result.knownValue = arg1.knownValue ^ arg2.knownValue; |
---|
[120867e] | 261 | } else if (fname == "?|?") { |
---|
[8f557161] | 262 | result.knownValue = arg1.knownValue | arg2.knownValue; |
---|
[120867e] | 263 | } else { |
---|
[8f557161] | 264 | result.isEvaluableInGCC = false; |
---|
| 265 | result.hasKnownValue = false; |
---|
[120867e] | 266 | } |
---|
| 267 | } // if |
---|
[d7aa12c] | 268 | // TODO: implement other intrinsic functions |
---|
| 269 | } |
---|
| 270 | }; |
---|
| 271 | |
---|
[120867e] | 272 | std::pair<long long int, bool> eval( const Expression * expr ) { |
---|
[d7aa12c] | 273 | PassVisitor<EvalOld> ev; |
---|
[120867e] | 274 | if ( expr ) { |
---|
| 275 | expr->accept( ev ); |
---|
| 276 | return std::make_pair( ev.pass.value, ev.pass.valid ); |
---|
[5cbacf1] | 277 | } else { |
---|
[120867e] | 278 | return std::make_pair( 0, false ); |
---|
[5cbacf1] | 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
[8f557161] | 282 | Evaluation eval( const ast::Expr * expr ) { |
---|
[120867e] | 283 | if ( expr ) { |
---|
[8f557161] | 284 | |
---|
| 285 | return ast::Pass<EvalNew>::read(expr); |
---|
| 286 | // Evaluation ret = ast::Pass<EvalNew>::read(expr); |
---|
| 287 | // ret.knownValue = 777; |
---|
| 288 | // return ret; |
---|
| 289 | |
---|
[d7aa12c] | 290 | } else { |
---|
[8f557161] | 291 | return { 0, false, false }; |
---|
[d7aa12c] | 292 | } |
---|
[0588d8c] | 293 | } |
---|
| 294 | |
---|
[5cbacf1] | 295 | // Local Variables: // |
---|
| 296 | // tab-width: 4 // |
---|
| 297 | // mode: c++ // |
---|
| 298 | // compile-command: "make install" // |
---|
| 299 | // End: // |
---|