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