[ee06e41b] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2018 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 | // prelude-gen.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Rob Schluntz and Thierry Delisle |
---|
| 10 | // Created On : Sat Feb 16 08:44:58 2019 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[0c81320] | 12 | // Last Modified On : Tue Mar 19 08:19:35 2019 |
---|
| 13 | // Update Count : 28 |
---|
[ee06e41b] | 14 | // |
---|
| 15 | |
---|
[1d386a7] | 16 | #include <algorithm> |
---|
| 17 | #include <array> |
---|
| 18 | #include <iostream> |
---|
| 19 | #include <string> |
---|
| 20 | #include <vector> |
---|
| 21 | using namespace std; |
---|
| 22 | |
---|
| 23 | static struct{ |
---|
| 24 | const string name; |
---|
| 25 | bool isFloat; |
---|
| 26 | bool hasComparison; |
---|
| 27 | } basicTypes[] = { |
---|
[ee06e41b] | 28 | { "char" , false, true , }, |
---|
| 29 | { "signed char" , false, true , }, |
---|
| 30 | { "unsigned char" , false, true , }, |
---|
[1d386a7] | 31 | { "signed short" , false, true , }, |
---|
| 32 | { "unsigned short" , false, true , }, |
---|
| 33 | { "signed int" , false, true , }, |
---|
| 34 | { "unsigned int" , false, true , }, |
---|
| 35 | { "signed long int" , false, true , }, |
---|
| 36 | { "unsigned long int" , false, true , }, |
---|
| 37 | { "signed long long int" , false, true , }, |
---|
| 38 | { "unsigned long long int", false, true , }, |
---|
| 39 | { "float" , true , true , }, |
---|
| 40 | { "double" , true , true , }, |
---|
| 41 | { "long double" , true , true , }, |
---|
| 42 | { "float _Complex" , true , false, }, |
---|
| 43 | { "double _Complex" , true , false, }, |
---|
| 44 | { "long double _Complex" , true , false, }, |
---|
| 45 | #if defined(__SIZEOF_INT128__) |
---|
| 46 | { "__int128" , false, true , }, |
---|
| 47 | { "unsigned __int128" , false, true , }, |
---|
| 48 | #endif |
---|
| 49 | #if defined(__i386__) || defined(__ia64__) || defined(__x86_64__) |
---|
| 50 | { "__float80" , true , true , }, |
---|
[2782f38] | 51 | { "__float128" , true , true , }, |
---|
[1d386a7] | 52 | #endif |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | struct { |
---|
| 56 | const string name; |
---|
| 57 | bool assignment = false; |
---|
| 58 | bool floatCompat = true; |
---|
| 59 | bool isComparison = false; |
---|
| 60 | bool isEqual = false; |
---|
| 61 | } arithmeticOperators[] = { |
---|
| 62 | { "?++" , true , true, false, false }, |
---|
| 63 | { "?--" , true , true, false, false }, |
---|
| 64 | { "++?" , true , true, false, false }, |
---|
| 65 | { "--?" , true , true, false, false }, |
---|
| 66 | { "+?" , false, true , false, false }, |
---|
| 67 | { "-?" , false, true , false, false }, |
---|
| 68 | { "~?" , false, false, false, false }, |
---|
| 69 | { "!?" , false, true , false, true }, |
---|
| 70 | { "?*?" , false, true , false, false }, |
---|
| 71 | { "?/?" , false, true , false, false }, |
---|
| 72 | { "?%?" , false, false, false, false }, |
---|
| 73 | { "?+?" , false, true , false, false }, |
---|
| 74 | { "?-?" , false, true , false, false }, |
---|
| 75 | { "?<<?" , false, false, false, false }, |
---|
| 76 | { "?>>?" , false, false, false, false }, |
---|
| 77 | { "?<?" , false, true , true , false }, |
---|
| 78 | { "?<=?" , false, true , true , true }, |
---|
| 79 | { "?>?" , false, true , true , false }, |
---|
| 80 | { "?>=?" , false, true , true , true }, |
---|
| 81 | { "?==?" , false, true , false, true }, |
---|
| 82 | { "?!=?" , false, true , false, true }, |
---|
| 83 | { "?&?" , false, false, false, false }, |
---|
| 84 | { "?^?" , false, false, false, false }, |
---|
| 85 | { "?|?" , false, false, false, false }, |
---|
| 86 | { "?=?" , true , true , false, false }, |
---|
| 87 | { "?+=?" , true , true , false, false }, |
---|
| 88 | { "?-=?" , true , true , false, false }, |
---|
| 89 | { "?*=?" , true , true , false, false }, |
---|
| 90 | { "?/=?" , true , true , false, false }, |
---|
| 91 | { "?%=?" , true , false, false, false }, |
---|
| 92 | { "?<<=?", true , false, false, false }, |
---|
| 93 | { "?>>=?", true , false, false, false }, |
---|
| 94 | { "?&=?" , true , false, false, false }, |
---|
| 95 | { "?|=?" , true , false, false, false }, |
---|
| 96 | { "?^=?" , true , false, false, false }, |
---|
| 97 | }; |
---|
| 98 | |
---|
| 99 | enum ArgType { Normal, PtrDiff, CommPtrDiff }; |
---|
| 100 | |
---|
| 101 | struct { |
---|
| 102 | const string name; |
---|
| 103 | bool assignment = false; |
---|
| 104 | string diffReturn; |
---|
| 105 | ArgType diffArg2 = Normal; |
---|
| 106 | string sized; |
---|
| 107 | } pointerOperators[] = { |
---|
| 108 | { "?++", true, "", Normal, " | sized(DT)" }, |
---|
| 109 | { "?--", true, "", Normal, " | sized(DT)" }, |
---|
| 110 | { "++?", true, "", Normal, " | sized(DT)" }, |
---|
| 111 | { "--?", true, "", Normal, " | sized(DT)" }, |
---|
| 112 | { "!?" , false, "int", Normal, "" }, |
---|
| 113 | { "?<?", false, "signed int", Normal, "" }, |
---|
| 114 | { "?<=?", false, "signed int", Normal, "" }, |
---|
| 115 | { "?>?", false, "signed int", Normal, "" }, |
---|
| 116 | { "?>=?", false, "signed int", Normal, "" }, |
---|
| 117 | { "?==?", false, "signed int", Normal, "" }, |
---|
| 118 | { "?!=?", false, "signed int", Normal, "" }, |
---|
| 119 | { "?=?", true, "", Normal, "" }, // void * LHS, zero_t RHS ??? |
---|
| 120 | { "*?", false, "&", Normal, " | sized(DT)" }, // & ??? |
---|
| 121 | |
---|
| 122 | { "?-?", false, "ptrdiff_t", Normal, " | sized(DT)" }, |
---|
| 123 | { "?-?", false, "", PtrDiff, " | sized(DT)" }, |
---|
| 124 | { "?-=?", true, "", PtrDiff, " | sized(DT)" }, |
---|
| 125 | |
---|
| 126 | { "?+?", false, "", CommPtrDiff, " | sized(DT)" }, |
---|
| 127 | { "?[?]", false, "&", CommPtrDiff, " | sized(DT)" }, // & ??? |
---|
| 128 | { "?+=?" , true, "", PtrDiff, " | sized(DT)" }, |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | template<size_t N> |
---|
| 132 | string mask2string(unsigned int mask, array<string, N> names) { |
---|
| 133 | string result = ""; |
---|
| 134 | int i = 0; |
---|
| 135 | for(auto name : names) { |
---|
| 136 | if(mask & (1 << i)) { |
---|
| 137 | result += name; |
---|
[1629965] | 138 | } else { |
---|
| 139 | result.append(name.size(), ' '); |
---|
[1d386a7] | 140 | } |
---|
| 141 | i++; |
---|
| 142 | } |
---|
| 143 | return result; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | template <typename... T> |
---|
| 147 | constexpr auto make_array(T&&... values) -> |
---|
| 148 | std::array< |
---|
| 149 | typename std::decay<typename std::common_type<T...>::type>::type, |
---|
| 150 | sizeof...(T)> |
---|
| 151 | { |
---|
| 152 | return std::array< |
---|
| 153 | typename std::decay< |
---|
| 154 | typename std::common_type<T...>::type>::type, |
---|
| 155 | sizeof...(T)>{{std::forward<T>(values)...}}; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | int main() { |
---|
[e523b07] | 159 | cout << "# 2 \"prelude.cfa\" // needed for error messages from this file" << endl; |
---|
[1d386a7] | 160 | cout << "trait sized(dtype T) {};" << endl; |
---|
| 161 | |
---|
| 162 | cout << "//////////////////////////" << endl; |
---|
| 163 | cout << "// Arithmetic Operators //" << endl; |
---|
| 164 | cout << "//////////////////////////" << endl; |
---|
| 165 | cout << endl; |
---|
| 166 | |
---|
[ee06e41b] | 167 | cout << "signed int ?==?( zero_t, zero_t ), ?!=?( zero_t, zero_t );" << endl; |
---|
| 168 | cout << "signed int ?==?( one_t, one_t ), ?!=?( one_t, one_t );" << endl; |
---|
| 169 | cout << "signed int ?==?( _Bool, _Bool ), ?!=?( _Bool, _Bool );" << endl; |
---|
[d9f0ed4] | 170 | cout << "signed int !?( _Bool );" << endl; |
---|
[1d386a7] | 171 | |
---|
| 172 | for (auto op : arithmeticOperators) { |
---|
| 173 | for (auto type : basicTypes ) { |
---|
| 174 | auto operands = count(op.name.begin(), op.name.end(), '?'); |
---|
| 175 | if (! op.floatCompat && type.isFloat) continue; |
---|
| 176 | if (op.isComparison && ! type.hasComparison) continue; |
---|
| 177 | if (op.assignment) { |
---|
| 178 | const char * qualifiers[] = { "", "volatile " }; |
---|
| 179 | for (auto q : qualifiers){ |
---|
| 180 | cout << type.name << " " << op.name << "("; |
---|
| 181 | cout << q << type.name << " &"; |
---|
| 182 | for (int i = 1; i < operands; ++i) { |
---|
| 183 | cout << ", " << type.name; |
---|
| 184 | } |
---|
| 185 | cout << ");" << endl; |
---|
| 186 | } |
---|
| 187 | } else { |
---|
| 188 | if (op.isComparison || op.isEqual) cout << "signed int"; |
---|
| 189 | else cout << type.name; |
---|
| 190 | cout << " " << op.name << "("; |
---|
| 191 | for (int i = 0; i < operands; ++i) { |
---|
| 192 | cout << type.name; |
---|
| 193 | if ((i+1) != operands) cout << ", "; |
---|
| 194 | } |
---|
| 195 | cout << ");" << endl; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | cout << endl; |
---|
| 199 | } |
---|
| 200 | cout << endl; |
---|
| 201 | |
---|
| 202 | cout << "/////////////////////////////" << endl; |
---|
| 203 | cout << "// Arithmetic Constructors //" << endl; |
---|
| 204 | cout << "/////////////////////////////" << endl; |
---|
[ee06e41b] | 205 | cout << endl; |
---|
| 206 | |
---|
[1629965] | 207 | auto otype = [](const std::string & type, bool do_volatile = false) { |
---|
[ee06e41b] | 208 | cout << "void ?{} (" << type << " &);" << endl; |
---|
| 209 | cout << "void ?{} (" << type << " &, " << type << ");" << endl; |
---|
[0c81320] | 210 | cout << type << " ?=? (" << type << " &, " << type << ")"; |
---|
[ee06e41b] | 211 | if ( do_volatile ) { |
---|
[0c81320] | 212 | cout << ", ?=?(volatile " << type << " &, " << type << ")"; |
---|
[1629965] | 213 | } |
---|
| 214 | cout << ";" << endl; |
---|
[ee06e41b] | 215 | cout << "void ^?{}( " << type << " & );" << endl; |
---|
[1629965] | 216 | }; |
---|
| 217 | |
---|
| 218 | otype("zero_t"); |
---|
[0c81320] | 219 | cout << endl; |
---|
[1629965] | 220 | otype("one_t"); |
---|
[0c81320] | 221 | cout << endl; |
---|
[1629965] | 222 | otype("_Bool", true); |
---|
[ee06e41b] | 223 | cout << endl; |
---|
[1629965] | 224 | |
---|
[1d386a7] | 225 | for (auto type : basicTypes) { |
---|
[ee06e41b] | 226 | cout << "void ?{}(" << type.name << " &);" << endl; |
---|
| 227 | cout << "void ?{}(" << type.name << " &, " << type.name << ");" << endl; |
---|
| 228 | cout << "void ?{}(" << type.name << " &, zero_t);" << endl; |
---|
[0c81320] | 229 | cout << "void ?{}(" << type.name << " &, one_t);" << endl; |
---|
[1d386a7] | 230 | cout << "void ^?{}(" << type.name << " &);" << endl; |
---|
| 231 | cout << endl; |
---|
| 232 | } |
---|
| 233 | cout << endl; |
---|
| 234 | |
---|
| 235 | cout << "//////////////////////////" << endl; |
---|
| 236 | cout << "// Pointer Constructors //" << endl; |
---|
| 237 | cout << "//////////////////////////" << endl; |
---|
[ee06e41b] | 238 | cout << endl; |
---|
| 239 | |
---|
| 240 | cout << "forall(ftype FT) void ?{}( FT *&, FT * );" << endl; |
---|
| 241 | cout << "forall(ftype FT) void ?{}( FT * volatile &, FT * );" << endl; |
---|
[1d386a7] | 242 | |
---|
[1629965] | 243 | // generate qualifiers |
---|
| 244 | vector<string> qualifiersSingle; |
---|
[1d386a7] | 245 | vector<pair<const string, const string>> qualifiersPair; |
---|
| 246 | const unsigned int NQ = 2; |
---|
| 247 | for(unsigned int lhs = 0; lhs < (1<<NQ); lhs++) { |
---|
[1629965] | 248 | // for parameter of default constructor and destructor |
---|
| 249 | qualifiersSingle.push_back(mask2string(lhs, make_array("const "s, "volatile "s))); |
---|
| 250 | |
---|
| 251 | // for first and second parameters of copy constructors |
---|
[1d386a7] | 252 | for(unsigned int rhs = 0; rhs < (1<<NQ); rhs++) { |
---|
| 253 | if((lhs & rhs) == rhs) { |
---|
| 254 | qualifiersPair.push_back({ |
---|
| 255 | mask2string(lhs, make_array("const "s, "volatile "s)), |
---|
| 256 | mask2string(rhs, make_array("const "s, "volatile "s)) |
---|
| 257 | }); |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
[1629965] | 262 | for (auto type : { " DT", "void" }) { |
---|
| 263 | for (auto cvq : qualifiersPair) { |
---|
| 264 | for (auto is_vol : { " ", "volatile" }) { |
---|
[ee06e41b] | 265 | cout << "forall(dtype DT) void ?{}(" << cvq.first << type << " * " << is_vol << " &, " << cvq.second << "DT *);" << endl; |
---|
[1629965] | 266 | } |
---|
| 267 | } |
---|
| 268 | for (auto cvq : qualifiersSingle) { |
---|
| 269 | for (auto is_vol : { " ", "volatile" }) { |
---|
[ee06e41b] | 270 | cout << "forall(dtype DT) void ?{}(" << cvq << type << " * " << is_vol << " &);" << endl; |
---|
[1629965] | 271 | } |
---|
| 272 | for (auto is_vol : { " ", "volatile" }) { |
---|
| 273 | cout << "forall(dtype DT) void ^?{}(" << cvq << type << " * " << is_vol << " &);" << endl; |
---|
| 274 | } |
---|
[1d386a7] | 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
[1629965] | 278 | { |
---|
| 279 | auto type = " DT"; |
---|
| 280 | for (auto is_vol : { " ", "volatile" }) { |
---|
| 281 | for (auto cvq : qualifiersSingle) { |
---|
| 282 | cout << "forall(dtype DT) void ?{}( " << cvq << type << " * " << is_vol << " &, zero_t);" << endl; |
---|
| 283 | } |
---|
[1d386a7] | 284 | } |
---|
| 285 | } |
---|
[1629965] | 286 | |
---|
[1d386a7] | 287 | cout << endl; |
---|
| 288 | |
---|
| 289 | cout << "forall(ftype FT) void ?{}( FT * &, zero_t ); " << endl; |
---|
[1629965] | 290 | cout << "forall(ftype FT) FT * ?=?( FT * &, zero_t );" << endl; |
---|
| 291 | cout << "forall(ftype FT) FT * ?=?( FT * volatile &, zero_t );" << endl; |
---|
[b830e046] | 292 | cout << "forall(ftype FT) void ?{}( FT * & );" << endl; |
---|
| 293 | cout << "forall(ftype FT) void ^?{}( FT * & );" << endl; |
---|
[1d386a7] | 294 | cout << endl; |
---|
| 295 | |
---|
| 296 | cout << "///////////////////////" << endl; |
---|
| 297 | cout << "// Pointer Operators //" << endl; |
---|
| 298 | cout << "///////////////////////" << endl; |
---|
| 299 | |
---|
[b830e046] | 300 | cout << "forall(ftype FT) FT * ?=?( FT *&, FT * );" << endl; |
---|
| 301 | cout << "forall(ftype FT) FT * ?=?( FT * volatile &, FT * );" << endl; |
---|
| 302 | cout << "forall(ftype FT) int !?( FT * );" << endl; |
---|
| 303 | cout << "forall(ftype FT) signed int ?==?( FT *, FT * );" << endl; |
---|
| 304 | cout << "forall(ftype FT) signed int ?!=?( FT *, FT * );" << endl; |
---|
| 305 | cout << "forall(ftype FT) FT & *?( FT * );" << endl; |
---|
[1d386a7] | 306 | |
---|
| 307 | for (auto op : pointerOperators) { |
---|
[1629965] | 308 | auto forall = [&op]() { |
---|
| 309 | cout << "forall(dtype DT" << op.sized << ") "; |
---|
| 310 | }; |
---|
[1d386a7] | 311 | for (auto type : { "DT"/*, "void"*/ } ) { |
---|
| 312 | auto operands = count(op.name.begin(), op.name.end(), '?'); |
---|
| 313 | if (op.assignment) { |
---|
| 314 | // const char * qualifiers[] = { "", "volatile ", "const ", "const volatile " }; |
---|
| 315 | switch(op.diffArg2) { |
---|
| 316 | case Normal: |
---|
| 317 | if (operands == 1) { |
---|
| 318 | for (auto q : qualifiersSingle){ |
---|
[1629965] | 319 | for (auto q2 : { " ", "volatile" }) { |
---|
| 320 | forall(); |
---|
[1d386a7] | 321 | cout << q << type << " * " << op.name << "("; |
---|
[1629965] | 322 | cout << q << type << " * " << q2 << " &"; |
---|
[1d386a7] | 323 | cout << ");" << endl; |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | } else { |
---|
| 327 | for (auto q : qualifiersPair){ |
---|
[1629965] | 328 | for (auto q2 : { " ", "volatile" }) { |
---|
| 329 | forall(); |
---|
[1d386a7] | 330 | cout << q.first << type << " * " << op.name << "("; |
---|
[1629965] | 331 | cout << q.first << type << " * " << q2 << " &"; |
---|
[1d386a7] | 332 | |
---|
| 333 | for (int i = 1; i < operands; ++i) { |
---|
| 334 | cout << ", " << q.second << type << " *"; |
---|
| 335 | } |
---|
| 336 | cout << ");" << endl; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | break; |
---|
| 341 | case PtrDiff: |
---|
| 342 | for (auto q : qualifiersSingle){ |
---|
[1629965] | 343 | for (auto q2 : { " ", "volatile" }) { |
---|
| 344 | forall(); |
---|
[1d386a7] | 345 | cout << q << type << " * " << op.name << "("; |
---|
[1629965] | 346 | cout << q << type << " * " << q2 << " &"; |
---|
[1d386a7] | 347 | |
---|
| 348 | for (int i = 1; i < operands; ++i) { |
---|
| 349 | cout << ", ptrdiff_t"; |
---|
| 350 | } |
---|
| 351 | cout << ");" << endl; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | break; |
---|
| 355 | default: |
---|
| 356 | abort(); |
---|
| 357 | } |
---|
| 358 | } else { |
---|
[1629965] | 359 | auto name_and_arg1 = [&op, &type](const std::string & q) { |
---|
| 360 | if (op.diffReturn == "&") cout << q << type << " &"; // -- qualifiers |
---|
| 361 | else if (op.diffReturn != "") cout << op.diffReturn; |
---|
| 362 | else cout << q << type << " *"; |
---|
| 363 | cout << " " << op.name << "("; |
---|
| 364 | }; |
---|
[1d386a7] | 365 | switch(op.diffArg2) { |
---|
| 366 | case Normal: |
---|
| 367 | for (auto q : qualifiersSingle) { |
---|
[1629965] | 368 | forall(); |
---|
| 369 | name_and_arg1( q ); |
---|
[1d386a7] | 370 | for (int i = 0; i < operands; ++i) { |
---|
| 371 | cout << q << type << " *"; |
---|
| 372 | if ((i+1) != operands) cout << ", "; |
---|
| 373 | } |
---|
| 374 | cout << ");" << endl; |
---|
| 375 | } |
---|
| 376 | break; |
---|
| 377 | case CommPtrDiff: |
---|
| 378 | for (auto q : qualifiersSingle) { |
---|
[1629965] | 379 | forall(); |
---|
| 380 | name_and_arg1( q ); |
---|
| 381 | cout << "ptrdiff_t, " << q << type << " *);" << endl; |
---|
[1d386a7] | 382 | } |
---|
| 383 | // fallthrough |
---|
| 384 | case PtrDiff: |
---|
| 385 | for (auto q : qualifiersSingle) { |
---|
[1629965] | 386 | forall(); |
---|
| 387 | name_and_arg1( q ); |
---|
| 388 | cout << q << type << " *, ptrdiff_t);" << endl; |
---|
[1d386a7] | 389 | } |
---|
| 390 | break; |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | cout << endl; |
---|
| 395 | } |
---|
| 396 | cout << endl; |
---|
| 397 | |
---|
[1629965] | 398 | for (auto is_vol : { " ", "volatile" }) { |
---|
| 399 | for (auto cvq : qualifiersPair) { |
---|
| 400 | cout << "forall(dtype DT) " << cvq.first << "void * ?=?( " << cvq.first << "void * " << is_vol << " &, " << cvq.second << "DT *);" << endl; |
---|
| 401 | } |
---|
| 402 | for (auto cvq : qualifiersSingle) { |
---|
| 403 | cout << "forall(dtype DT) " << cvq << " DT * ?=?( " << cvq << " DT * " << is_vol << " &, zero_t);" << endl; |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | cout << endl; |
---|
[1d386a7] | 407 | } |
---|
[ee06e41b] | 408 | |
---|
| 409 | // Local Variables: // |
---|
| 410 | // tab-width: 4 // |
---|
| 411 | // End: // |
---|