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