source: src/BasicTypes-gen.cc@ ec28948

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since ec28948 was c2fe922, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

remove path from generating file name

  • Property mode set to 100644
File size: 17.5 KB
Line 
1#include <queue>
2#include <iostream>
3#include <iomanip>
4#include <fstream>
5#include <utility>
6#include <string>
7using namespace std;
8#include <assert.h>
9#include <string.h> // strlen
10#include "config.h" // configure info
11
12enum Kind {
13 Bool,
14 Char,
15 SignedChar,
16 UnsignedChar,
17 ShortSignedInt,
18 ShortUnsignedInt,
19 SignedInt,
20 UnsignedInt,
21 LongSignedInt,
22 LongUnsignedInt,
23 LongLongSignedInt,
24 LongLongUnsignedInt,
25 SignedInt128,
26 UnsignedInt128,
27 uFloat16,
28 uFloat16Complex,
29 uFloat32,
30 uFloat32Complex,
31 Float,
32 FloatComplex,
33 // FloatImaginary,
34 uFloat32x,
35 uFloat32xComplex,
36 uFloat64,
37 uFloat64Complex,
38 Double,
39 DoubleComplex,
40 // DoubleImaginary,
41 uFloat64x,
42 uFloat64xComplex,
43 uuFloat80,
44 uFloat128,
45 uFloat128Complex,
46 uuFloat128,
47 LongDouble,
48 LongDoubleComplex,
49 // LongDoubleImaginary,
50 uFloat128x,
51 uFloat128xComplex,
52 NUMBER_OF_BASIC_TYPES
53};
54
55enum NumSort { // floating point types act as both signed and unsigned
56 Signed = 0x1,
57 Unsigned = 0x2,
58 Floating = 0x3
59};
60
61struct Node {
62 Kind basicType; // basic type
63 const char * name; // basic-type name
64 const char * abbrev; // internal abbreviation (documentation only)
65 const char * type; // actual type name
66 const char * mangled; // mangled abbreviation
67 NumSort sign; // is this a signed integral type?
68 int left, middle, right; // 3-ary tree, -1 => nullptr
69 int rank; // integral rank (C standard 6.3.1.1.1, extended)
70} graph[NUMBER_OF_BASIC_TYPES] = {
71 { Bool, "Bool", "B", "_Bool", "b", Signed, Char, SignedChar, -1, 0 }, // root
72
73 { Char, "Char", "C", "char", "c", Signed, SignedChar, UnsignedChar, ShortSignedInt, 1 },
74 { SignedChar, "SignedChar", "SC", "signed char", "a", Signed, UnsignedChar, ShortSignedInt, -1, 1 },
75 { UnsignedChar, "UnsignedChar", "UC", "unsigned char", "h", Unsigned, ShortUnsignedInt, ShortSignedInt, -1, 1 },
76
77 { ShortSignedInt, "ShortSignedInt", "SI", "signed short int", "s", Signed, ShortUnsignedInt, SignedInt, -1, 2 },
78 { ShortUnsignedInt, "ShortUnsignedInt", "SUI", "unsigned short int", "t", Unsigned, UnsignedInt, SignedInt, -1, 2 },
79
80 { SignedInt, "SignedInt", "I", "signed int", "i", Signed, UnsignedInt, LongSignedInt, -1, 3 },
81 { UnsignedInt, "UnsignedInt", "UI", "unsigned int", "j", Unsigned, LongUnsignedInt, LongSignedInt, -1, 3 },
82
83 { LongSignedInt, "LongSignedInt", "LI", "signed long int", "l", Signed, LongUnsignedInt, LongLongSignedInt, -1, 4 },
84 { LongUnsignedInt, "LongUnsignedInt", "LUI", "unsigned long int", "m", Unsigned, LongLongSignedInt, LongLongUnsignedInt, -1, 4 },
85
86 { LongLongSignedInt, "LongLongSignedInt", "LLI", "signed long long int", "x", Signed, LongLongUnsignedInt, SignedInt128, -1, 5 },
87 { LongLongUnsignedInt, "LongLongUnsignedInt", "LLUI", "unsigned long long int", "y", Unsigned, SignedInt128, UnsignedInt128, -1, 5 },
88
89 { SignedInt128, "SignedInt128", "IB", "__int128", "n", Signed, UnsignedInt128, uFloat16, -1, 6 },
90 { UnsignedInt128, "UnsignedInt128", "UIB", "unsigned __int128", "o", Unsigned, uFloat16, -1, -1, 6 },
91
92 { uFloat16, "uFloat16", "_FH", "_Float16", "DF16_", Floating, uFloat32, uFloat16Complex, -1, 7 },
93 { uFloat16Complex, "uFloat16Complex", "_FH", "_Float16 _Complex", "CDF16_", Floating, uFloat32Complex, -1, -1, 7 },
94 { uFloat32, "uFloat32", "_F", "_Float32", "DF32_", Floating, Float, uFloat32Complex, -1, 8 },
95 { uFloat32Complex, "uFloat32Complex", "_FC", "_Float32 _Complex", "CDF32_", Floating, FloatComplex, -1, -1, 8 },
96 { Float, "Float", "F", "float", "f", Floating, uFloat32x, FloatComplex, -1, 9 },
97 { FloatComplex, "FloatComplex", "FC", "float _Complex", "Cf", Floating, uFloat32xComplex, -1, -1, 9 },
98 // { FloatImaginary, "FloatImaginary", "FI", "float _Imaginary", "If", false, DoubleImaginary, FloatComplex, -1, 9 },
99
100 { uFloat32x, "uFloat32x", "_FX", "_Float32x", "DF32x_", Floating, uFloat64, uFloat32xComplex, -1, 10 },
101 { uFloat32xComplex, "uFloat32xComplex", "_FXC", "_Float32x _Complex", "CDF32x_", Floating, uFloat64Complex, -1, -1, 10 },
102 { uFloat64, "uFloat64", "FD", "_Float64", "DF64_", Floating, Double, uFloat64Complex, -1, 11 },
103 { uFloat64Complex, "uFloat64Complex", "_FDC", "_Float64 _Complex", "CDF64_", Floating, DoubleComplex, -1, -1, 11 },
104 { Double, "Double", "D", "double", "d", Floating, uFloat64x, DoubleComplex, -1, 12 },
105 { DoubleComplex, "DoubleComplex", "DC", "double _Complex", "Cd", Floating, uFloat64xComplex, -1, -1, 12 },
106 // { DoubleImaginary, "DoubleImaginary", "DI", "double _Imaginary", "Id", false, LongDoubleImaginary, DoubleComplex, -1, 12 },
107
108 { uFloat64x, "uFloat64x", "F80X", "_Float64x", "DF64x_", Floating, uuFloat80, uFloat64xComplex, -1, 13 },
109 { uFloat64xComplex, "uFloat64xComplex", "_FDXC", "_Float64x _Complex", "CDF64x_", Floating, uFloat128Complex, -1, -1, 13 },
110 { uuFloat80, "uuFloat80", "F80", "__float80", "Dq", Floating, uFloat128, uFloat64xComplex, -1, 14 },
111 { uFloat128, "uFloat128", "_FB", "_Float128", "DF128_", Floating, uuFloat128, uFloat128Complex, -1, 15 },
112 { uFloat128Complex, "uFloat128Complex", "_FLDC", "_Float128 _Complex", "CDF128_", Floating, LongDoubleComplex, -1, -1, 15 },
113 { uuFloat128, "uuFloat128", "FB", "__float128", "g", Floating, LongDouble, uFloat128Complex, -1, 16 },
114 { LongDouble, "LongDouble", "LD", "long double", "e", Floating, uFloat128x, LongDoubleComplex, -1, 17 },
115 { LongDoubleComplex, "LongDoubleComplex", "LDC", "long double _Complex", "Ce", Floating, uFloat128xComplex, -1, -1, 17 },
116 // { LongDoubleImaginary, "LongDoubleImaginary", "LDI", "long double _Imaginary", "Ie", false, LongDoubleComplex, -1, -1, 17 },
117
118 { uFloat128x, "uFloat128x", "_FBX", "_Float128x", "DF128x_", Floating, uFloat128xComplex, -1, -1, 18 },
119 { uFloat128xComplex, "uFloat128xComplex", "_FLDXC", "_Float128x _Complex", "CDF128x_", Floating, -1, -1, -1, 18 }
120}; // graph
121
122static int costMatrix[NUMBER_OF_BASIC_TYPES][NUMBER_OF_BASIC_TYPES];
123static int signMatrix[NUMBER_OF_BASIC_TYPES][NUMBER_OF_BASIC_TYPES];
124static Kind commonTypeMatrix[NUMBER_OF_BASIC_TYPES][NUMBER_OF_BASIC_TYPES];
125
126void generateCosts( int row ) {
127 bool seen[NUMBER_OF_BASIC_TYPES] = { false /*, ... */ };
128
129 struct el_cost {
130 int i;
131 int path;
132 int sign;
133
134 el_cost( int i = 0, int p = 0, int s = 0 ) : i(i), path(p), sign(s) {}
135
136 // reverse the sense for use in largest-on-top priority queue
137 bool operator< (const el_cost& o) const {
138 return path > o.path || (path == o.path && sign > o.sign);
139 }
140 };
141
142 // initialize BFS queue with root of traversal
143 priority_queue< el_cost > q;
144 q.emplace( row, 0, 0 );
145
146 // BFS costs
147 do {
148 // visit cost element
149 int col = q.top().i;
150 // skip if already set
151 if ( seen[col] ) {
152 q.pop();
153 continue;
154 } else {
155 seen[col] = true;
156 } // if
157
158 // otherwise set min-costs into matrix
159 int cost = q.top().path;
160 int scost = q.top().sign;
161 costMatrix[row][col] = cost;
162 signMatrix[row][col] = scost;
163 q.pop();
164
165 // traverse children
166 int i = graph[col].left;
167 if ( i == -1 ) continue;
168 q.emplace( i, cost + 1, scost + ! (graph[col].sign & graph[i].sign) );
169
170 i = graph[col].middle;
171 if ( i == -1 ) continue;
172 q.emplace( i, cost + 1, scost + !(graph[col].sign & graph[i].sign) );
173
174 i = graph[col].right;
175 if ( i == -1 ) continue;
176 q.emplace( i, cost + 1, scost + !(graph[col].sign & graph[i].sign) );
177 } while ( ! q.empty() );
178} // generateCosts
179
180void generateCommonType( int row, int col ) { // row <= col
181 if ( costMatrix[row][col] >= 0 ) {
182 // safe conversion from row => col
183 commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[col].basicType;
184 } else if ( costMatrix[col][row] >= 0 ) {
185 // safe conversion from col => row
186 commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[row].basicType;
187 } else {
188 // need to find least common ancestor
189 // can cheat a bit here, in that there is always a direct ancestor of the later (col) element
190 int i = graph[col].left;
191 if ( i == -1 ) assert("invalid ancestor assumption");
192 if ( costMatrix[row][i] >= 0 ) {
193 commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[i].basicType;
194 return;
195 } // if
196
197 i = graph[col].middle;
198 if ( i == -1 ) assert("invalid ancestor assumption");
199 if ( costMatrix[row][i] >= 0 ) {
200 commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[i].basicType;
201 return;
202 } // if
203
204 i = graph[col].right;
205 if ( i == -1 ) assert("invalid ancestor assumption");
206 if ( costMatrix[row][i] >= 0 ) {
207 commonTypeMatrix[row][col] = commonTypeMatrix[col][row] = graph[i].basicType;
208 return;
209 } // if
210
211 assert("invalid ancestor assumption");
212 } // if
213} // generateCommonType
214
215void resetInput( fstream & file, const char * filename, stringstream & buffer, stringstream & code, string & str ) {
216 file.close();
217 buffer.str( "" );
218 code.str( "" );
219 file.open( filename, fstream::in );
220 if ( file.fail() ) {
221 cout << "Internal error, could not open " << filename << " for input." << endl;
222 abort();
223 } // if
224 buffer << file.rdbuf();
225 str = buffer.str();
226} // resetInput
227
228void output( fstream & file, const char * filename, stringstream & code ) {
229 file.close();
230 file.open( filename, fstream::out );
231 if ( file.fail() ) {
232 cout << "Internal error, could not open " << filename << " for output." << endl;
233 abort();
234 } // if
235 file << code.rdbuf(); // overwrite file
236} // output
237
238void Abort( const char * kind, const char * file ) {
239 cerr << "Internal error, could not find " << kind << " of generated code for " << file << endl;
240} // Abort
241
242int main() {
243 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // initialization
244 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
245 costMatrix[r][c] = -1;
246 signMatrix[r][c] = -1;
247 commonTypeMatrix[r][c] = NUMBER_OF_BASIC_TYPES;
248 } // for
249 } // for
250
251 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // perform breath-first traversal to generate cost graph
252 generateCosts(r);
253 } // for
254
255 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // use cost graph to find nearest-common-ancestor
256 for (int c = r; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
257 generateCommonType(r, c);
258 } // for
259 } // for
260
261 #define STARTMK "// GENERATED START, DO NOT EDIT"
262 #define ENDMK "// GENERATED END"
263 string BYMK( __FILE__ );
264 string::size_type posn = BYMK.find_last_of( "/" );
265 if ( posn != string::npos ) BYMK.erase( 0, posn - 1); // remove directories
266 BYMK = "// GENERATED BY " + BYMK;
267
268 fstream file;
269 stringstream buffer, code;
270 string str;
271 size_t start, end;
272
273
274 #define Type TOP_SRCDIR "src/SynTree/Type.h"
275 resetInput( file, Type, buffer, code, str );
276
277 if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", Type );
278 start += sizeof( STARTMK ); // includes newline
279 code << str.substr( 0, start );
280
281 code << "\t" << BYMK << endl;
282 code << "\tenum Kind {" << endl;
283 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
284 code << "\t\t" << graph[r].name << "," << endl;
285 } // for
286 code << "\t\tNUMBER_OF_BASIC_TYPES" << endl;
287 code << "\t} kind;" << endl;
288 code << "\t"; // indentation for end marker
289
290 if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", Type );
291 code << str.substr( start );
292
293 output( file, Type, code );
294 // cout << code.str();
295
296
297 #define ConversionCost TOP_SRCDIR "src/ResolvExpr/ConversionCost.cc"
298 resetInput( file, ConversionCost, buffer, code, str );
299
300 if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ConversionCost );
301 start += sizeof( STARTMK ); // includes newline
302 code << str.substr( 0, start );
303
304 code << "\t" << BYMK << endl;
305 code << "\t/* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves)" << endl;
306 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
307 code << '\t' << left;
308 if ( graph[c].rank != graph[c + 1].rank ) {
309 code << right << setw(30) << graph[c].type << left;
310 } else if ( graph[c].rank != graph[c + 2].rank ) {
311 code << string( 10, ' ' ) << setw(25) << graph[c].type << setw(25) << graph[c + 1].type;
312 c += 1;
313 } else {
314 code << setw(20) << graph[c].type << setw(20) << graph[c + 1].type << setw(20) << graph[c + 2].type;
315 c += 2;
316 } // if
317 code << endl;
318 } // for
319 code << right << "\t*/" << endl;
320 code << "\t"; // indentation for end marker
321
322 if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
323 if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
324 end += sizeof( STARTMK );
325 code << str.substr( start, end - start );
326
327 code << "\t" << BYMK << endl;
328 code << "\tstatic const int costMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // path length from root to node" << endl
329 << "\t\t/* ";
330 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
331 code << setw(5) << graph[r].abbrev;
332 } // for
333 code << " */" << endl;
334 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
335 code << "\t\t/*" << setw(6) << graph[r].abbrev << "*/ {";
336 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
337 code << setw(4) << costMatrix[r][c] << ",";
338 } // for
339 code << " }," << endl;
340 } // for
341 code << "\t}; // costMatrix" << endl;
342 code << "\t"; // indentation for end marker
343
344 if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
345 if ( (end = str.find( STARTMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
346 end += sizeof( STARTMK );
347 code << str.substr( start, end - start );
348
349 code << "\t" << BYMK << endl;
350 code << "\tstatic const int signMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion" << endl
351 << "\t\t/* ";
352 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
353 code << setw(5) << graph[r].abbrev;
354 } // for
355 code << " */" << endl;
356 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
357 code << "\t\t/*" << setw(6) << graph[r].abbrev << "*/ {";
358 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
359 code << setw(4) << signMatrix[r][c] << ",";
360 } // for
361 code << " }," << endl;
362 } // for
363 code << "\t}; // signMatrix" << endl;
364 code << "\t"; // indentation for end marker
365
366 if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
367 code << str.substr( start );
368
369 output( file, ConversionCost, code );
370 // cout << code.str();
371
372
373 #define CommonType TOP_SRCDIR "src/ResolvExpr/CommonType.cc"
374 resetInput( file, CommonType, buffer, code, str );
375
376 if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", CommonType );
377 start += sizeof( STARTMK ); // includes newline
378 code << str.substr( 0, start );
379
380 enum { PER_ROW = 6 };
381 code << "\t" << BYMK << endl;
382 code << "\t#define BT BasicType::" << endl;
383 code << "\tstatic const BasicType::Kind commonTypes[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor" << endl
384 << "\t\t/*\t\t ";
385 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
386 code << setw(24) << graph[r].abbrev;
387 if ( (r+1) % PER_ROW == 0 ) {
388 code << endl << "\t\t\t\t ";
389 } // if
390 } // for
391 code << "*/" << endl;
392 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
393 code << "\t\t\t\t {\n\t\t/*" << setw(6) << graph[r].abbrev << "*/";
394 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
395 string s = string{"BT "} + graph[commonTypeMatrix[r][c]].name;
396 code << setw(23) << s << ",";
397 if ( (c+1) % PER_ROW == 0 ) {
398 code << endl << "\t\t\t\t ";
399 } // if
400 } // for
401 code << "}," << endl;
402 } // for
403 code << "\t}; // commonTypes" << endl;
404 code << "\t#undef BT" << endl;
405 code << "\t"; // indentation for end marker
406
407 if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", CommonType );
408 code << str.substr( start );
409
410 output( file, CommonType, code );
411 // cout << code.str();
412
413
414 #define ManglerCommon TOP_SRCDIR "src/SymTab/ManglerCommon.cc"
415 resetInput( file, ManglerCommon, buffer, code, str );
416
417 if ( (start = str.find( STARTMK )) == string::npos ) Abort( "start", ManglerCommon );
418 start += sizeof( STARTMK ); // includes newline
419 code << str.substr( 0, start );
420
421 code << "\t\t\t// GENERATED BY " __FILE__ << endl;
422 code <<
423 "\t\t\t// NOTES ON MANGLING:\n"
424 "\t\t\t// * Itanium spec says that Float80 encodes to \"e\" (like LongDouble), but the distinct lengths cause resolution problems.\n"
425 "\t\t\t// * Float128 is supposed to encode to \"g\", but I wanted it to mangle equal to LongDouble.\n"
426 "\t\t\t// * Mangling for non-standard complex types is by best guess\n"
427 "\t\t\t// * _FloatN is supposed to encode as \"DF\"N\"_\"; modified for same reason as above.\n"
428 "\t\t\t// * unused mangling identifiers:\n"
429 "\t\t\t// - \"z\" ellipsis\n"
430 "\t\t\t// - \"Dd\" IEEE 754r 64-bit decimal floating point (borrowed for _Float32x)\n"
431 "\t\t\t// - \"De\" IEEE 754r 128-bit decimal floating point\n"
432 "\t\t\t// - \"Df\" IEEE 754r 32-bit decimal floating point\n"
433 "\t\t\t// - \"Dh\" IEEE 754r 16-bit decimal floating point (borrowed for _Float16)\n"
434 "\t\t\t// - \"DF\"N\"_\" ISO/IEC TS 18661 N-bit binary floating point (_FloatN)\n"
435 "\t\t\t// - \"Di\" char32_t\n"
436 "\t\t\t// - \"Ds\" char16_t\n";
437
438 code << "\t\t\tconst std::string basicTypes[BasicType::NUMBER_OF_BASIC_TYPES] = {" << endl;
439 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
440 code << "\t\t\t\t\"" << graph[r].mangled << "\"," << setw(9 - strlen(graph[r].mangled)) << ' ' << "// " << graph[r].type << endl;
441 } // for
442 code << "\t\t\t}; // basicTypes" << endl;
443 code << "\t\t\t"; // indentation for end marker
444
445 if ( (start = str.find( ENDMK, start + 1 )) == string::npos ) Abort( "end", ManglerCommon );
446 code << str.substr( start );
447
448 output( file, ManglerCommon, code );
449 // cout << code.str();
450} // main
451
452// Local Variables: //
453// tab-width: 4 //
454// mode: c++ //
455// compile-command: "g++-8 -Wall -Wextra BasicTypes-gen.cc" //
456// End: //
Note: See TracBrowser for help on using the repository browser.