source: src/BasicTypes-gen.cc@ 70a3e16

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 70a3e16 was 70a3e16, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add libtool

  • 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 endMK "// GENERATED END"
253 fstream file;
254 stringstream buffer, code;
255 string str;
256 size_t start, end;
257
258
259 #define Type TOP_SRCDIR "src/SynTree/Type.h"
260 resetInput( file, Type, buffer, code, str );
261
262 if ( (start = str.find( startMK )) == string::npos ) Abort( "start", Type );
263 start += sizeof( startMK ); // includes newline
264 code << str.substr( 0, start );
265
266 code << "\t// GENERATED BY " __FILE__ << endl;
267 code << "\tenum Kind {" << endl;
268 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
269 code << "\t\t" << graph[r].name << "," << endl;
270 } // for
271 code << "\t\tNUMBER_OF_BASIC_TYPES" << endl;
272 code << "\t} kind;" << endl;
273 code << "\t"; // indentation for end marker
274
275 if ( (start = str.find( endMK, start + 1 )) == string::npos ) Abort( "end", Type );
276 code << str.substr( start );
277
278 output( file, Type, code );
279 // cout << code.str();
280
281
282 #define ConversionCost TOP_SRCDIR "src/ResolvExpr/ConversionCost.cc"
283 resetInput( file, ConversionCost, buffer, code, str );
284
285 if ( (start = str.find( startMK )) == string::npos ) Abort( "start", ConversionCost );
286 start += sizeof( startMK ); // includes newline
287 code << str.substr( 0, start );
288
289 code << "\t// GENERATED BY " __FILE__ << endl;
290 code << "\t/* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves)" << endl;
291 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
292 code << '\t' << left;
293 if ( graph[c].rank != graph[c + 1].rank ) {
294 code << right << setw(30) << graph[c].type << left;
295 } else if ( graph[c].rank != graph[c + 2].rank ) {
296 code << string( 10, ' ' ) << setw(25) << graph[c].type << setw(25) << graph[c + 1].type;
297 c += 1;
298 } else {
299 code << setw(20) << graph[c].type << setw(20) << graph[c + 1].type << setw(20) << graph[c + 2].type;
300 c += 2;
301 } // if
302 code << endl;
303 } // for
304 code << right << "\t*/" << endl;
305 code << "\t"; // indentation for end marker
306
307 if ( (start = str.find( endMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
308 if ( (end = str.find( startMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
309 end += sizeof( startMK );
310 code << str.substr( start, end - start );
311
312 code << "\t// GENERATED BY " __FILE__ << endl;
313 code << "\tstatic const int costMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // path length from root to node" << endl
314 << "\t\t/* ";
315 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
316 code << setw(4) << graph[r].abbrev << ' ';
317 } // for
318 code << "*/" << endl;
319 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
320 code << "\t\t/*" << setw(6) << graph[r].abbrev << "*/";
321 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
322 code << setw(3) << costMatrix[r][c] << ", ";
323 } // for
324 code << endl;
325 } // for
326 code << "\t}; // costMatrix" << endl;
327 code << "\t"; // indentation for end marker
328
329 if ( (start = str.find( endMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
330 if ( (end = str.find( startMK, start + 1 )) == string::npos ) Abort( "start", ConversionCost );
331 end += sizeof( startMK );
332 code << str.substr( start, end - start );
333
334 code << "\t// GENERATED BY " __FILE__ << endl;
335 code << "\tstatic const int signMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion" << endl
336 << "\t\t/* ";
337 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
338 code << setw(4) << graph[r].abbrev << ' ';
339 } // for
340 code << "*/" << endl;
341 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
342 code << "\t\t/*" << setw(6) << graph[r].abbrev << "*/";
343 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
344 code << setw(3) << signMatrix[r][c] << ", ";
345 } // for
346 code << endl;
347 } // for
348 code << "\t}; // signMatrix" << endl;
349 code << "\t"; // indentation for end marker
350
351 if ( (start = str.find( endMK, start + 1 )) == string::npos ) Abort( "end", ConversionCost );
352 code << str.substr( start );
353
354 output( file, ConversionCost, code );
355 // cout << code.str();
356
357
358 #define CommonType TOP_SRCDIR "src/ResolvExpr/CommonType.cc"
359 resetInput( file, CommonType, buffer, code, str );
360
361 if ( (start = str.find( startMK )) == string::npos ) Abort( "start", CommonType );
362 start += sizeof( startMK ); // includes newline
363 code << str.substr( 0, start );
364
365 enum { PER_ROW = 6 };
366 code << "\t// GENERATED BY " __FILE__ << endl;
367 code << "\t#define BT BasicType::" << endl;
368 code << "\tstatic const BasicType::Kind commonTypes[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor" << endl
369 << "\t\t/* ";
370 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // titles
371 code << setw(24) << graph[r].abbrev;
372 if ( (r+1) % PER_ROW == 0 ) {
373 code << endl << "\t\t ";
374 } else {
375 code << ' ';
376 }
377 } // for
378 code << "*/" << endl;
379 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) { // costs
380 code << "\t\t/*" << setw(6) << graph[r].abbrev << "*/";
381 for ( int c = 0; c < NUMBER_OF_BASIC_TYPES; c += 1 ) {
382 string s = string{"BT "} + graph[commonTypeMatrix[r][c]].name;
383 code << setw(23) << s << ",";
384 if ( (c+1) % PER_ROW == 0 ) {
385 code << endl << "\t\t ";
386 } else {
387 code << ' ';
388 }
389 } // for
390 code << endl;
391 } // for
392 code << "\t};" << endl;
393 code << "\t#undef BT" << endl;
394 code << "\t"; // indentation for end marker
395
396 if ( (start = str.find( endMK, start + 1 )) == string::npos ) Abort( "end", CommonType );
397 code << str.substr( start );
398
399 output( file, CommonType, code );
400 // cout << code.str();
401
402
403 #define ManglerCommon TOP_SRCDIR "src/SymTab/ManglerCommon.cc"
404 resetInput( file, ManglerCommon, buffer, code, str );
405
406 if ( (start = str.find( startMK )) == string::npos ) Abort( "start", ManglerCommon );
407 start += sizeof( startMK ); // includes newline
408 code << str.substr( 0, start );
409
410 code << "\t\t\t// GENERATED BY " __FILE__ << endl;
411 code <<
412 "\t\t\t// NOTES ON MANGLING:\n"
413 "\t\t\t// * Itanium spec says that Float80 encodes to \"e\" (like LongDouble), but the distinct lengths cause resolution problems.\n"
414 "\t\t\t// * Float128 is supposed to encode to \"g\", but I wanted it to mangle equal to LongDouble.\n"
415 "\t\t\t// * Mangling for non-standard complex types is by best guess\n"
416 "\t\t\t// * _FloatN is supposed to encode as \"DF\"N\"_\"; modified for same reason as above.\n"
417 "\t\t\t// * unused mangling identifiers:\n"
418 "\t\t\t// - \"z\" ellipsis\n"
419 "\t\t\t// - \"Dd\" IEEE 754r 64-bit decimal floating point (borrowed for _Float32x)\n"
420 "\t\t\t// - \"De\" IEEE 754r 128-bit decimal floating point\n"
421 "\t\t\t// - \"Df\" IEEE 754r 32-bit decimal floating point\n"
422 "\t\t\t// - \"Dh\" IEEE 754r 16-bit decimal floating point (borrowed for _Float16)\n"
423 "\t\t\t// - \"DF\"N\"_\" ISO/IEC TS 18661 N-bit binary floating point (_FloatN)\n"
424 "\t\t\t// - \"Di\" char32_t\n"
425 "\t\t\t// - \"Ds\" char16_t\n";
426
427 code << "\t\t\tconst std::string basicTypes[BasicType::NUMBER_OF_BASIC_TYPES] = {" << endl;
428 for ( int r = 0; r < NUMBER_OF_BASIC_TYPES; r += 1 ) {
429 code << "\t\t\t\t\"" << graph[r].mangled << "\"," << setw(9 - strlen(graph[r].mangled)) << ' ' << "// " << graph[r].type << endl;
430 } // for
431 code << "\t\t\t}; // basicTypes" << endl;
432 code << "\t\t\t"; // indentation for end marker
433
434 if ( (start = str.find( endMK, start + 1 )) == string::npos ) Abort( "end", ManglerCommon );
435 code << str.substr( start );
436
437 output( file, ManglerCommon, code );
438 // cout << code.str();
439} // main
440
441// Local Variables: //
442// tab-width: 4 //
443// mode: c++ //
444// compile-command: "g++-8 -Wall -Wextra BasicTypes-gen.cc" //
445// End: //
Note: See TracBrowser for help on using the repository browser.