source: src/BasicTypes-gen.cc@ 0050a5f

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

more bracket rows in matrix initialization

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