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