[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 | // |
---|
[c92bdcc] | 7 | // Eval.cpp -- 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 | |
---|
[c92bdcc] | 16 | #include "Eval.hpp" |
---|
[8f06277] | 17 | |
---|
[5cbacf1] | 18 | #include <utility> // for pair |
---|
| 19 | |
---|
[e01eb4a] | 20 | #include "AST/Inspect.hpp" |
---|
[c92bdcc] | 21 | #include "CodeGen/OperatorTable.hpp" // access: OperatorInfo |
---|
[d7aa12c] | 22 | #include "AST/Pass.hpp" |
---|
[c92bdcc] | 23 | #include "InitTweak/InitTweak.hpp" |
---|
[5cbacf1] | 24 | |
---|
[d7aa12c] | 25 | struct EvalNew : public ast::WithShortCircuiting { |
---|
[8f557161] | 26 | Evaluation result = { 0, true, true }; |
---|
[d7aa12c] | 27 | |
---|
| 28 | void previsit( const ast::Node * ) { visit_children = false; } |
---|
[8f557161] | 29 | void postvisit( const ast::Node * ) { result.isEvaluableInGCC = result.hasKnownValue = false; } |
---|
[d7aa12c] | 30 | |
---|
[120867e] | 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 |
---|
[8f557161] | 36 | result.knownValue = expr->intValue(); |
---|
| 37 | result.hasKnownValue = true; |
---|
| 38 | result.isEvaluableInGCC = true; |
---|
[d7aa12c] | 39 | } |
---|
| 40 | |
---|
[120867e] | 41 | void postvisit( const ast::SizeofExpr * ) { |
---|
[8f557161] | 42 | result.hasKnownValue = false; |
---|
| 43 | result.isEvaluableInGCC = true; |
---|
[120867e] | 44 | } |
---|
| 45 | |
---|
| 46 | void postvisit( const ast::AlignofExpr * ) { |
---|
[8f557161] | 47 | result.hasKnownValue = false; |
---|
| 48 | result.isEvaluableInGCC = true; |
---|
[120867e] | 49 | } |
---|
| 50 | |
---|
| 51 | void postvisit( const ast::OffsetofExpr * ) { |
---|
[8f557161] | 52 | result.hasKnownValue = false; |
---|
| 53 | result.isEvaluableInGCC = true; |
---|
[77d2432] | 54 | } |
---|
| 55 | |
---|
[120867e] | 56 | void postvisit( const ast::LogicalExpr * expr ) { |
---|
[8f557161] | 57 | Evaluation arg1, arg2; |
---|
[120867e] | 58 | arg1 = eval( expr->arg1 ); |
---|
[8f557161] | 59 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 60 | if ( ! result.isEvaluableInGCC ) return; |
---|
[120867e] | 61 | arg2 = eval( expr->arg2 ); |
---|
[8f557161] | 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; |
---|
[120867e] | 68 | |
---|
| 69 | if ( expr->isAnd ) { |
---|
[8f557161] | 70 | result.knownValue = arg1.knownValue && arg2.knownValue; |
---|
[120867e] | 71 | } else { |
---|
[8f557161] | 72 | result.knownValue = arg1.knownValue || arg2.knownValue; |
---|
[120867e] | 73 | } // if |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void postvisit( const ast::ConditionalExpr * expr ) { |
---|
[8f557161] | 77 | Evaluation arg1, arg2, arg3; |
---|
[120867e] | 78 | arg1 = eval( expr->arg1 ); |
---|
[8f557161] | 79 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 80 | if ( ! result.isEvaluableInGCC ) return; |
---|
[120867e] | 81 | arg2 = eval( expr->arg2 ); |
---|
[8f557161] | 82 | result.isEvaluableInGCC &= arg2.isEvaluableInGCC; |
---|
| 83 | if ( ! result.isEvaluableInGCC ) return; |
---|
[120867e] | 84 | arg3 = eval( expr->arg3 ); |
---|
[8f557161] | 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; |
---|
[120867e] | 92 | |
---|
[8f557161] | 93 | result.knownValue = arg1.knownValue ? arg2.knownValue : arg3.knownValue; |
---|
[120867e] | 94 | } |
---|
| 95 | |
---|
| 96 | void postvisit( const ast::CastExpr * expr ) { |
---|
[8f557161] | 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); |
---|
[d7aa12c] | 100 | } |
---|
| 101 | |
---|
[120867e] | 102 | void postvisit( const ast::VariableExpr * expr ) { |
---|
[8f557161] | 103 | result.hasKnownValue = false; |
---|
| 104 | result.isEvaluableInGCC = false; |
---|
[d7aa12c] | 105 | if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) { |
---|
| 106 | if ( const ast::EnumDecl * decl = inst->base ) { |
---|
[8f557161] | 107 | result.isEvaluableInGCC = true; |
---|
| 108 | result.hasKnownValue = decl->valueOf( expr->var, result.knownValue ); // result.knownValue filled by valueOf |
---|
[d7aa12c] | 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | void postvisit( const ast::ApplicationExpr * expr ) { |
---|
[e01eb4a] | 114 | const ast::DeclWithType * function = ast::getFunction(expr); |
---|
[8f557161] | 115 | if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { |
---|
| 116 | result.isEvaluableInGCC = false; |
---|
| 117 | result.hasKnownValue = false; |
---|
| 118 | return; |
---|
| 119 | } |
---|
[d7aa12c] | 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() ); |
---|
[120867e] | 122 | |
---|
| 123 | if ( expr->args.size() == 1 ) { |
---|
| 124 | // pre/postfix operators ++ and -- => assignment, which is not constant |
---|
[8f557161] | 125 | Evaluation arg1; |
---|
[120867e] | 126 | arg1 = eval(expr->args.front()); |
---|
[8f557161] | 127 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 128 | if ( ! result.isEvaluableInGCC ) return; |
---|
| 129 | |
---|
| 130 | result.hasKnownValue &= arg1.hasKnownValue; |
---|
| 131 | if ( ! result.hasKnownValue ) return; |
---|
[120867e] | 132 | |
---|
| 133 | if (fname == "+?") { |
---|
[8f557161] | 134 | result.knownValue = arg1.knownValue; |
---|
[120867e] | 135 | } else if (fname == "-?") { |
---|
[8f557161] | 136 | result.knownValue = -arg1.knownValue; |
---|
[120867e] | 137 | } else if (fname == "~?") { |
---|
[8f557161] | 138 | result.knownValue = ~arg1.knownValue; |
---|
[120867e] | 139 | } else if (fname == "!?") { |
---|
[8f557161] | 140 | result.knownValue = ! arg1.knownValue; |
---|
[120867e] | 141 | } else { |
---|
[8f557161] | 142 | result.isEvaluableInGCC = false; |
---|
| 143 | result.hasKnownValue = false; |
---|
[120867e] | 144 | } // if |
---|
| 145 | } else { // => expr->args.size() == 2 |
---|
| 146 | // infix assignment operators => assignment, which is not constant |
---|
[8f557161] | 147 | Evaluation arg1, arg2; |
---|
[120867e] | 148 | arg1 = eval(expr->args.front()); |
---|
[8f557161] | 149 | result.isEvaluableInGCC &= arg1.isEvaluableInGCC; |
---|
| 150 | if ( ! result.isEvaluableInGCC ) return; |
---|
[d7aa12c] | 151 | arg2 = eval(expr->args.back()); |
---|
[8f557161] | 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; |
---|
[120867e] | 158 | |
---|
| 159 | if (fname == "?+?") { |
---|
[8f557161] | 160 | result.knownValue = arg1.knownValue + arg2.knownValue; |
---|
[120867e] | 161 | } else if (fname == "?-?") { |
---|
[8f557161] | 162 | result.knownValue = arg1.knownValue - arg2.knownValue; |
---|
[120867e] | 163 | } else if (fname == "?*?") { |
---|
[8f557161] | 164 | result.knownValue = arg1.knownValue * arg2.knownValue; |
---|
[120867e] | 165 | } else if (fname == "?/?") { |
---|
[8f557161] | 166 | if ( arg2.knownValue ) result.knownValue = arg1.knownValue / arg2.knownValue; |
---|
[120867e] | 167 | } else if (fname == "?%?") { |
---|
[8f557161] | 168 | if ( arg2.knownValue ) result.knownValue = arg1.knownValue % arg2.knownValue; |
---|
[120867e] | 169 | } else if (fname == "?<<?") { |
---|
[8f557161] | 170 | result.knownValue = arg1.knownValue << arg2.knownValue; |
---|
[120867e] | 171 | } else if (fname == "?>>?") { |
---|
[8f557161] | 172 | result.knownValue = arg1.knownValue >> arg2.knownValue; |
---|
[120867e] | 173 | } else if (fname == "?<?") { |
---|
[8f557161] | 174 | result.knownValue = arg1.knownValue < arg2.knownValue; |
---|
[120867e] | 175 | } else if (fname == "?>?") { |
---|
[8f557161] | 176 | result.knownValue = arg1.knownValue > arg2.knownValue; |
---|
[120867e] | 177 | } else if (fname == "?<=?") { |
---|
[8f557161] | 178 | result.knownValue = arg1.knownValue <= arg2.knownValue; |
---|
[120867e] | 179 | } else if (fname == "?>=?") { |
---|
[8f557161] | 180 | result.knownValue = arg1.knownValue >= arg2.knownValue; |
---|
[120867e] | 181 | } else if (fname == "?==?") { |
---|
[8f557161] | 182 | result.knownValue = arg1.knownValue == arg2.knownValue; |
---|
[120867e] | 183 | } else if (fname == "?!=?") { |
---|
[8f557161] | 184 | result.knownValue = arg1.knownValue != arg2.knownValue; |
---|
[120867e] | 185 | } else if (fname == "?&?") { |
---|
[8f557161] | 186 | result.knownValue = arg1.knownValue & arg2.knownValue; |
---|
[120867e] | 187 | } else if (fname == "?^?") { |
---|
[8f557161] | 188 | result.knownValue = arg1.knownValue ^ arg2.knownValue; |
---|
[120867e] | 189 | } else if (fname == "?|?") { |
---|
[8f557161] | 190 | result.knownValue = arg1.knownValue | arg2.knownValue; |
---|
[120867e] | 191 | } else { |
---|
[8f557161] | 192 | result.isEvaluableInGCC = false; |
---|
| 193 | result.hasKnownValue = false; |
---|
[120867e] | 194 | } |
---|
| 195 | } // if |
---|
[d7aa12c] | 196 | // TODO: implement other intrinsic functions |
---|
| 197 | } |
---|
| 198 | }; |
---|
| 199 | |
---|
[8f557161] | 200 | Evaluation eval( const ast::Expr * expr ) { |
---|
[120867e] | 201 | if ( expr ) { |
---|
[8f557161] | 202 | |
---|
| 203 | return ast::Pass<EvalNew>::read(expr); |
---|
| 204 | // Evaluation ret = ast::Pass<EvalNew>::read(expr); |
---|
| 205 | // ret.knownValue = 777; |
---|
| 206 | // return ret; |
---|
| 207 | |
---|
[d7aa12c] | 208 | } else { |
---|
[8f557161] | 209 | return { 0, false, false }; |
---|
[d7aa12c] | 210 | } |
---|
[0588d8c] | 211 | } |
---|
| 212 | |
---|
[5cbacf1] | 213 | // Local Variables: // |
---|
| 214 | // tab-width: 4 // |
---|
| 215 | // mode: c++ // |
---|
| 216 | // compile-command: "make install" // |
---|
| 217 | // End: // |
---|