source: src/prelude/prelude-gen.cc@ 46e480a5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 46e480a5 was 1d386a7, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Prelude is now generated

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