1 | #include <algorithm> |
---|
2 | #include <array> |
---|
3 | #include <iostream> |
---|
4 | #include <string> |
---|
5 | #include <vector> |
---|
6 | using namespace std; |
---|
7 | |
---|
8 | static 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 | |
---|
40 | struct { |
---|
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 | |
---|
84 | enum ArgType { Normal, PtrDiff, CommPtrDiff }; |
---|
85 | |
---|
86 | struct { |
---|
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 | |
---|
116 | template<size_t N> |
---|
117 | string 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 | } else { |
---|
124 | result.append(name.size(), ' '); |
---|
125 | } |
---|
126 | i++; |
---|
127 | } |
---|
128 | return result; |
---|
129 | } |
---|
130 | |
---|
131 | template <typename... T> |
---|
132 | constexpr auto make_array(T&&... values) -> |
---|
133 | std::array< |
---|
134 | typename std::decay<typename std::common_type<T...>::type>::type, |
---|
135 | sizeof...(T)> |
---|
136 | { |
---|
137 | return std::array< |
---|
138 | typename std::decay< |
---|
139 | typename std::common_type<T...>::type>::type, |
---|
140 | sizeof...(T)>{{std::forward<T>(values)...}}; |
---|
141 | } |
---|
142 | |
---|
143 | int main() { |
---|
144 | cout << "# 2 \"prelude.cf\" // needed for error messages from this file" << endl; |
---|
145 | cout << "trait sized(dtype T) {};" << endl; |
---|
146 | |
---|
147 | cout << "//////////////////////////" << endl; |
---|
148 | cout << "// Arithmetic Operators //" << endl; |
---|
149 | cout << "//////////////////////////" << endl; |
---|
150 | cout << endl; |
---|
151 | |
---|
152 | cout << "signed int ?==?( zero_t, zero_t ), ?!=?( zero_t, zero_t );" << endl; |
---|
153 | cout << "signed int ?==?( one_t, one_t ), ?!=?( one_t, one_t );" << endl; |
---|
154 | cout << "signed int ?==?( _Bool, _Bool ), ?!=?( _Bool, _Bool );" << endl; |
---|
155 | cout << "signed int !?( _Bool );" << endl; |
---|
156 | |
---|
157 | for (auto op : arithmeticOperators) { |
---|
158 | for (auto type : basicTypes ) { |
---|
159 | auto operands = count(op.name.begin(), op.name.end(), '?'); |
---|
160 | if (! op.floatCompat && type.isFloat) continue; |
---|
161 | if (op.isComparison && ! type.hasComparison) continue; |
---|
162 | if (op.assignment) { |
---|
163 | const char * qualifiers[] = { "", "volatile " }; |
---|
164 | for (auto q : qualifiers){ |
---|
165 | cout << type.name << " " << op.name << "("; |
---|
166 | cout << q << type.name << " &"; |
---|
167 | for (int i = 1; i < operands; ++i) { |
---|
168 | cout << ", " << type.name; |
---|
169 | } |
---|
170 | cout << ");" << endl; |
---|
171 | } |
---|
172 | } else { |
---|
173 | if (op.isComparison || op.isEqual) cout << "signed int"; |
---|
174 | else cout << type.name; |
---|
175 | cout << " " << op.name << "("; |
---|
176 | for (int i = 0; i < operands; ++i) { |
---|
177 | cout << type.name; |
---|
178 | if ((i+1) != operands) cout << ", "; |
---|
179 | } |
---|
180 | cout << ");" << endl; |
---|
181 | } |
---|
182 | } |
---|
183 | cout << endl; |
---|
184 | } |
---|
185 | cout << endl; |
---|
186 | |
---|
187 | cout << "/////////////////////////////" << endl; |
---|
188 | cout << "// Arithmetic Constructors //" << endl; |
---|
189 | cout << "/////////////////////////////" << endl; |
---|
190 | auto otype = [](const std::string & type, bool do_volatile = false) { |
---|
191 | cout << "void \t?{} ( " << type << " & );" << endl; |
---|
192 | cout << "void \t?{} ( " << type << " &, " << type << " );" << endl; |
---|
193 | cout << type << " \t?=? ( " << type << " &, " << type << " )"; |
---|
194 | if( do_volatile ) { |
---|
195 | cout << ", \t?=?( volatile " << type << " &, " << type << " )"; |
---|
196 | } |
---|
197 | cout << ";" << endl; |
---|
198 | cout << "void \t^?{}( " << type << " & );" << endl; |
---|
199 | }; |
---|
200 | |
---|
201 | otype("zero_t"); |
---|
202 | otype("one_t"); |
---|
203 | otype("_Bool", true); |
---|
204 | otype("char", true); |
---|
205 | otype("signed char", true); |
---|
206 | otype("unsigned char", true); |
---|
207 | |
---|
208 | for (auto type : basicTypes) { |
---|
209 | cout << "void ?{}(" << type.name << " &);" << endl; |
---|
210 | cout << "void ?{}(" << type.name << " &, " << type.name << ");" << endl; |
---|
211 | cout << "void ^?{}(" << type.name << " &);" << endl; |
---|
212 | cout << endl; |
---|
213 | } |
---|
214 | cout << endl; |
---|
215 | |
---|
216 | cout << "//////////////////////////" << endl; |
---|
217 | cout << "// Pointer Constructors //" << endl; |
---|
218 | cout << "//////////////////////////" << endl; |
---|
219 | cout << "forall(ftype FT) void ?{}( FT *&, FT * );" << endl; |
---|
220 | cout << "forall(ftype FT) void ?{}( FT * volatile &, FT * );" << endl; |
---|
221 | |
---|
222 | // generate qualifiers |
---|
223 | vector<string> qualifiersSingle; |
---|
224 | vector<pair<const string, const string>> qualifiersPair; |
---|
225 | const unsigned int NQ = 2; |
---|
226 | for(unsigned int lhs = 0; lhs < (1<<NQ); lhs++) { |
---|
227 | // for parameter of default constructor and destructor |
---|
228 | qualifiersSingle.push_back(mask2string(lhs, make_array("const "s, "volatile "s))); |
---|
229 | |
---|
230 | // for first and second parameters of copy constructors |
---|
231 | for(unsigned int rhs = 0; rhs < (1<<NQ); rhs++) { |
---|
232 | if((lhs & rhs) == rhs) { |
---|
233 | qualifiersPair.push_back({ |
---|
234 | mask2string(lhs, make_array("const "s, "volatile "s)), |
---|
235 | mask2string(rhs, make_array("const "s, "volatile "s)) |
---|
236 | }); |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | for (auto type : { " DT", "void" }) { |
---|
242 | for (auto cvq : qualifiersPair) { |
---|
243 | for (auto is_vol : { " ", "volatile" }) { |
---|
244 | cout << "forall(dtype DT) void ?{}(" << cvq.first << type << " * " << is_vol << " &, " << cvq.second << "DT *);" << endl; |
---|
245 | } |
---|
246 | } |
---|
247 | for (auto cvq : qualifiersSingle) { |
---|
248 | for (auto is_vol : { " ", "volatile" }) { |
---|
249 | cout << "forall(dtype DT) void ?{}(" << cvq << type << " * " << is_vol << " &);" << endl; |
---|
250 | } |
---|
251 | for (auto is_vol : { " ", "volatile" }) { |
---|
252 | cout << "forall(dtype DT) void ^?{}(" << cvq << type << " * " << is_vol << " &);" << endl; |
---|
253 | } |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | { |
---|
258 | auto type = " DT"; |
---|
259 | for (auto is_vol : { " ", "volatile" }) { |
---|
260 | for (auto cvq : qualifiersSingle) { |
---|
261 | cout << "forall(dtype DT) void ?{}( " << cvq << type << " * " << is_vol << " &, zero_t);" << endl; |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | cout << endl; |
---|
267 | |
---|
268 | cout << "forall(ftype FT) void ?{}( FT * &, zero_t ); " << endl; |
---|
269 | cout << "forall(ftype FT) FT * ?=?( FT * &, zero_t );" << endl; |
---|
270 | cout << "forall(ftype FT) FT * ?=?( FT * volatile &, 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 | for (auto op : pointerOperators) { |
---|
288 | auto forall = [&op]() { |
---|
289 | cout << "forall(dtype DT" << op.sized << ") "; |
---|
290 | }; |
---|
291 | for (auto type : { "DT"/*, "void"*/ } ) { |
---|
292 | auto operands = count(op.name.begin(), op.name.end(), '?'); |
---|
293 | if (op.assignment) { |
---|
294 | // const char * qualifiers[] = { "", "volatile ", "const ", "const volatile " }; |
---|
295 | switch(op.diffArg2) { |
---|
296 | case Normal: |
---|
297 | if (operands == 1) { |
---|
298 | for (auto q : qualifiersSingle){ |
---|
299 | for (auto q2 : { " ", "volatile" }) { |
---|
300 | forall(); |
---|
301 | cout << q << type << " * " << op.name << "("; |
---|
302 | cout << q << type << " * " << q2 << " &"; |
---|
303 | cout << ");" << endl; |
---|
304 | } |
---|
305 | } |
---|
306 | } else { |
---|
307 | for (auto q : qualifiersPair){ |
---|
308 | for (auto q2 : { " ", "volatile" }) { |
---|
309 | forall(); |
---|
310 | cout << q.first << type << " * " << op.name << "("; |
---|
311 | cout << q.first << type << " * " << q2 << " &"; |
---|
312 | |
---|
313 | for (int i = 1; i < operands; ++i) { |
---|
314 | cout << ", " << q.second << type << " *"; |
---|
315 | } |
---|
316 | cout << ");" << endl; |
---|
317 | } |
---|
318 | } |
---|
319 | } |
---|
320 | break; |
---|
321 | case PtrDiff: |
---|
322 | for (auto q : qualifiersSingle){ |
---|
323 | for (auto q2 : { " ", "volatile" }) { |
---|
324 | forall(); |
---|
325 | cout << q << type << " * " << op.name << "("; |
---|
326 | cout << q << type << " * " << q2 << " &"; |
---|
327 | |
---|
328 | for (int i = 1; i < operands; ++i) { |
---|
329 | cout << ", ptrdiff_t"; |
---|
330 | } |
---|
331 | cout << ");" << endl; |
---|
332 | } |
---|
333 | } |
---|
334 | break; |
---|
335 | default: |
---|
336 | abort(); |
---|
337 | } |
---|
338 | } else { |
---|
339 | auto name_and_arg1 = [&op, &type](const std::string & q) { |
---|
340 | if (op.diffReturn == "&") cout << q << type << " &"; // -- qualifiers |
---|
341 | else if (op.diffReturn != "") cout << op.diffReturn; |
---|
342 | else cout << q << type << " *"; |
---|
343 | cout << " " << op.name << "("; |
---|
344 | }; |
---|
345 | switch(op.diffArg2) { |
---|
346 | case Normal: |
---|
347 | for (auto q : qualifiersSingle) { |
---|
348 | forall(); |
---|
349 | name_and_arg1( q ); |
---|
350 | for (int i = 0; i < operands; ++i) { |
---|
351 | cout << q << type << " *"; |
---|
352 | if ((i+1) != operands) cout << ", "; |
---|
353 | } |
---|
354 | cout << ");" << endl; |
---|
355 | } |
---|
356 | break; |
---|
357 | case CommPtrDiff: |
---|
358 | for (auto q : qualifiersSingle) { |
---|
359 | forall(); |
---|
360 | name_and_arg1( q ); |
---|
361 | cout << "ptrdiff_t, " << q << type << " *);" << endl; |
---|
362 | } |
---|
363 | // fallthrough |
---|
364 | case PtrDiff: |
---|
365 | for (auto q : qualifiersSingle) { |
---|
366 | forall(); |
---|
367 | name_and_arg1( q ); |
---|
368 | cout << q << type << " *, ptrdiff_t);" << endl; |
---|
369 | } |
---|
370 | break; |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | cout << endl; |
---|
375 | } |
---|
376 | cout << endl; |
---|
377 | |
---|
378 | for (auto is_vol : { " ", "volatile" }) { |
---|
379 | for (auto cvq : qualifiersPair) { |
---|
380 | cout << "forall(dtype DT) " << cvq.first << "void * ?=?( " << cvq.first << "void * " << is_vol << " &, " << cvq.second << "DT *);" << endl; |
---|
381 | } |
---|
382 | for (auto cvq : qualifiersSingle) { |
---|
383 | cout << "forall(dtype DT) " << cvq << " DT * ?=?( " << cvq << " DT * " << is_vol << " &, zero_t);" << endl; |
---|
384 | } |
---|
385 | } |
---|
386 | cout << endl; |
---|
387 | } |
---|
388 | |
---|