source: libcfa/prelude/prelude-gen.cc@ 7592f42

Last change on this file since 7592f42 was ef05cf0, checked in by Andrew Beach <ajbeach@…>, 5 months ago

Moved over some clean-up I did in various attempted fixes. InitDepthChecker has a very rare error case that has now been removed.

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